I have many lists which looks like
['it']
['was']
['annoying']
I want the above to look like
['it', 'was', 'annoying']
How do I achieve that?
philipxy
14.5k5 gold badges33 silver badges79 bronze badges
asked Jul 20, 2012 at 6:48
user1452759user1452759
8,03613 gold badges40 silver badges54 bronze badges
3
import itertools
ab = itertools.chain[['it'], ['was'], ['annoying']]
list[ab]
Just another method....
answered Jul 20, 2012 at 6:58
8
Just add them:
['it'] + ['was'] + ['annoying']
You should read the Python tutorial to learn basic info like this.
answered Jul 20, 2012 at 6:49
BrenBarnBrenBarn
232k35 gold badges396 silver badges374 bronze badges
7
a = ['it']
b = ['was']
c = ['annoying']
a.extend[b]
a.extend[c]
# a now equals ['it', 'was', 'annoying']
answered Jul 20, 2012 at 6:50
ScottyScotty
2,4022 gold badges15 silver badges19 bronze badges
1
Not the answer you're looking for? Browse other questions tagged python list merge or ask your own question.
In this article we will discuss different ways to Merge / Join two or more lists in python. Table of Contents Suppose we have two lists i.e. We want to merge the contents of these two list into a single list i.e. There are different ways to do this. Let’s discuss them by one by one. Advertisements In python, we can use the + operator to merge the contents of two lists into a new list. For example, # List of strings
list1 = ["This" , "is", "a", "sample", "program"]
# List of ints
list2 = [10, 2, 45, 3, 5, 7, 8, 10]
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
Join / Merge two lists in python using + operator
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.