Join list in Python

In this article we will discuss different ways to Merge / Join two or more lists in python.

Table of Contents

  • Join / Merge two lists in python using + operator.
  • Join / Merge two lists in python using list.extend[].
  • Join / Merge two lists in python using unpacking.
  • Join / Merge two lists in python using itertools.
  • Join / Merge two lists in python using for loop.
  • Join / Merge multiple lists in python.

Suppose we have two lists i.e.

# List of strings list1 = ["This" , "is", "a", "sample", "program"] # List of ints list2 = [10, 2, 45, 3, 5, 7, 8, 10]

We want to merge the contents of these two list into a single list i.e.

['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

There are different ways to do this. Lets discuss them by one by one.

Advertisements

Join / Merge two lists in python using + operator

In python, we can use the + operator to merge the contents of two lists into a new list. For example,

We can use + operator to merge two lists i.e.

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Merge two lists final_list = list_1 + list_2 print['Merged List:'] print[final_list]

Output:

Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It returned a new concatenated lists, which contains the contents of both list_1 and list_2. Whereas, list_1 and list_2 remained same as original.

Join / Merge two lists in python using list.extend[]

In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend[] function of list i.e.

list.extend[anotherList]

list.extend[] makes a list longer by appending the elements of another list at the end of the calling list object. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Makes list1 longer by appending the elements of list2 at the end. list_1.extend[list_2] print['Merged List:'] print[list_1]

Output

Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It extended the list_1 by adding the contents of list_2 at the end of list_1.

Join / Merge two lists in python using unpacking

In python, we can unpack the contents on any iterable object using the * operator. So, *list will unpack the contents of a list. We can unpack the contents of both the lists and create a new list with the merged contents. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Merge two lists final_list = [*list_1, *list_2] print['Merged List:'] print[final_list]

Output:

Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It unpacked the contents of both the lists and created a new list with the contents of both the lists.

Join / Merge two lists in python using itertools.chain[]

In python, the itertools module provides a function chain[] to merge the contents of multiple iterable sequences,

itertools.chain[*iterables]

It creates a chain of all the iterable sequences passed as arguments and returns an iterator.

This iterator returns the elements from first iterable sequence until it is exhausted and then moves to the next iterable. We can use this iterator to created a merged list of contents. For example,

import itertools # List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Join two lists final_list=list[itertools.chain[list_1, list_2]] print['Merged List:'] print[final_list]

Output:

Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Join / Merge two lists in python using for loop

We can iterate over all elements of a list using for loop and during iteration we can append each element to an another list. This way we can extend the contents of a list. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Iterate over a list and add elements to another list for elem in list_2: list_1.append[elem] print['Extended List:'] print[list_1]

Output:

Extended List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

We iterated over all the elements in list_2 and while iteration added each element at the end of list_1. Therefore list_1 is now extended and contains the contents of both the lists i.e. original list_1 and list_2.

Join / Merge multiple lists using + operator

We can merge the contents of multiple lists to a new list using the + operator. For example,

list_1 = ["This" , "is", "a", "sample", "program"] list_2 = [10, 2, 45, 3, 5, 7, 8, 10] list_3 = [11, 12, 13] # Merge 3 lists into a single list merged_list = list_1 + list_2 + list_3 print['Merged List:'] print[merged_list]

Output:

Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

Conclusion:

We learned about different ways to join or merge multiple lists in python.

Pandas Tutorials -Learn Data Analysis with Python

  • Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
  • Pandas Tutorial Part #2 - Basics of Pandas Series
  • Pandas Tutorial Part #3 - Get & Set Series values
  • Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
  • Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
  • Pandas Tutorial Part #6 - Introduction to DataFrame
  • Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
  • Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
  • Pandas Tutorial Part #9 - Filter DataFrame Rows
  • Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
  • Pandas Tutorial Part #11 - DataFrame attributes & methods
  • Pandas Tutorial Part #12 - Handling Missing Data or NaN values
  • Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
  • Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
  • Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
  • Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Video liên quan

Chủ Đề