You are currently viewing Python Join Lists (Two or Multiple Lists)

How to join two or multiple lists in Python? There are multiple ways to join two lists in Python, in this article, we will see how to join the elements within the list using join() method and join two or multiple lists.

Advertisements

The following are methods to join lists in Python:

  1. Join two lists using the list append() method
  2. Join two lists using the list extend() method
  3. Join two or multiple lists using itertools.chain()
  4. Join two or multiple lists using + operator

1. Quick Examples of Joining Lists

Following are quick examples of how to join elements within the list and join two or multiple lists.


# Consider three lists
social=['history','civics','economics','geography']
languages=['hindi','english']
others=['subject1']

# Let's join elements within a List
# Example 1: Using join(), Join strings in a list with '-' delimiter.
print('-'.join(social) )

# Example 2: Using join() with map(), with '-' delimiter.
print('-'.join(map(str,social)))

# Let's join two lists
# Example 1: Using append()
for i in languages:
    social.append(i) 
print("Joined Lists: ",social)

# Example 2: Using extend()
social.extend(languages) 
print("Joined Lists: ",social)

# Let's join two or more lists

# Example 1: Using itertools.chain()
import itertools
print("Joined Lists: ",list(itertools.chain (social,languages,others)))

# Example 2: Using * operator
print("Joined Lists: ",[*social, *languages])

# Example 3: Using + operator
print("Joined Lists: ",social+languages+others)

2. Join two Lists in Python

To join two lists in Python you can use either the append() or extend() methods. The first method joins one list with another list but the joining list will be added as a single element. Whereas the second method actually extends the elements from the list and adds each element to the list. Let’s see each one with an example.

2.1 Using extend()

Using list.extend() is the straight and easiest approach to joining lists in Python. This creates a single list with all elements from both lists. This doesn’t return anything instead it updates the original list. By using this method you can join elements from iterable like sets, and tuples to the list.

2.1.1 extend() Syntax

Let’s see how to join lists using extend() method.


# Syntax
mylist1.extend(mylist2) 

Here, mylist1 is the first list and mylist2 is the second list.

2.1.2 extend() Example

Let’s create two lists named languages1 and languages2. Join both lists by appending the languages2 list to the languages1 list.


# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)

# Using extend() to join lists
languages1.extend(languages2)
print("Joined Lists: ",languages1)

This example yields the below output.

join lists in python

2.2 append() to Join Lists

The list.append() is also used to join elements at the end of the list. Here, the element can be a string, number, iterator e.t.c. When you join a list with another list by using this, it actually adds the entire list as an element hence, I will use the for loop to get each element of the list and append this element to the list inside the loop.

2.2.1 append() Syntax

Let’s see how to join two lists using the append() method.


# Syntax
for iterator in mylist2:
    mylist1.append(iterator) 

Here, mylist1 is the first list and mylist2 is the second list.

2.2.2 append() Example

Let’s create two lists named languages1 and languages2. Join both lists by appending the languages2 list to the languages1 list.


# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)

# Using append()
for i in languages2:
    languages1.append(i) 
print("Joined Lists: ",languages1)

This example yields the below output.

python join lists

3. Join Multiple Lists in Python

In the above section, I have explained how to join two lists now, let’s see how to join multiple lists in Python. To join multiple lists you can use itertools.chain() or + operator. Both these approaches join all values from multiple lists to a single list.

3.1 Using itertools.chain()

The itertools module in python is used to iterate over the given data structures. Use chain() in itertools module to join the given iterables. It takes all iterable you would like to join as an argument and returns an itertools.chain, you need to convert this to list using list().

3.1.1 itertools.chain() Syntax

Let’s see how to use itertools.chain() method.


# Syntax
list(itertools.chain(list1,list2,....))

3.1.2 itertools.chain() Example

Let’s create three lists and join them using itertools.chain() method.


import itertools  

# Consider three lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
languages3=["Scala","Ruby"]
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)
print("Language List 3: ",languages3)

# Join multiple lists
result = list(itertools.chain(languages1,languages2,languages3))
print("Joined Lists: ",result)

This example joins all 3 lists and yields the below output.

python join multiple lists

3.2 Using + operator

‘+’ operator will join two or more lists. You just need to specify this operator in between the lists.

3.2.1 Syntax

Let’s see how to use the ‘+’ operator to join multiple lists.


# Using '+' operator
list1+list2+....

3.2.2 Example

Let’s create three lists and join them using ‘+’ operator.


# Join multiple lists
result = languages1 + languages2 + languages3
print("Joined Lists: ",result)

This yields the same output as above.

3.3 Using * operator

‘*’ operator is an unpacking operator which will unpack the elements in the lists and join the lists. To join lists, we need to specify this operator before each list and specify all the lists inside [].

3.3.1 Syntax

Let’s see how to use the ‘*’ operator to join multiple lists.


# Using '*' operator
[*list1, *list2, ...]

3.3.2 Example

Let’s create three lists and join them using ‘*’ operator.


# Join multiple lists
result = [*languages1, *languages2, *languages3]
print("Joined Lists: ",result)

This yields the same output as above.

4. Join Elements within the List using join()

In this scenario, we will see how to join elements within a list together into a string. The join() is used to convert list to string. We can specify the delimiter along with the join() method such that elements are joined with this delimiter.

4.1 join() Syntax

Let’s see how to join the elements in the list by using join() and map() with join().

Here, map() will take two parameters, First parameter is the datatype i.e string and second parameter is the list name.


# Here, mylist1 is the input list.
'delimiter'.join(mylist1) 

# join() with map()
'delimiter'.join(map(str,mylist1))

4.2 Examples

Example 1: Let’s create a list of books and join each book with a delimiter – “-” using join().


# Consider the list of books
books=['history','civics','economics','geography']
print("Book List: ",books)

# Using join(), Join strings in a list with '-' delimiter.
print('-'.join(books) )

# Output:
# Book List:  ['history', 'civics', 'economics', 'geography']
# history-civics-economics-geography

We had 4 books on a list. After joining them, the joined string is : “history-civics-economics-geography”.

5. Conclusion

In this article, you have learned different methods to join two and multiple python lists into single list. To join two lists, we used list.append() and list.extend() methods. If you want to join two or multiple lists at a time, we used itertools.chain() method, ‘*’ and ‘+’ operators.

Related Article