This question already has answers here:
Combining 2 lists in python
(2 answers)
Closed 8 years ago.
If I have 2 lists:
list1 = ["X","Y","Z"]
list2 = [1,2,3]
How can I combine them to make a another list:
list3 = [[1,"X"],[2,"Y"],[3,"Z"]]
Thanks in advance!
Just use zip to get tuples of values at corresponding indices in the two lists, and then cast each tuple to a list. So:
[list(t) for t in zip(list1, list2)]
is all you need to do.
Demo:
>>> list1 = ["X", "Y", "Z"]
>>> list2 = [1, 2, 3]
>>> list3 = [list(t) for t in zip(list1, list2)]
>>> list3
[[1, 'X'], [2, 'Y'], [3, 'Z']]
Related
This question already has answers here:
How to zip two differently sized lists, repeating the shorter list?
(15 answers)
Closed 26 days ago.
I want to combine two lists in an alternating way in Python.
list1 = ['A']
list2 = [1, 2, 3, 4]
What I've tried:
combination = [x for y in zip(list1, list2) for x in y]
# Output:
['A', 1]
# Expected output:
['A', 1, 'A', 2, 'A', 3, 'A', 4]
How can I combine these two lists and get the expected output? (Preferably list comprehension)
One way is to use itertools cycle function:
from itertools import cycle
list1 = ['A']
list2 = [1, 2, 3, 4]
combination = [x for y in zip(cycle(list1), list2) for x in y]
This also works with
list1 = ['A', 'B']
list1*len(list2) will repeat the first list elements as many times as the length of the second list.
combination = [x for y in zip(list1*len(list2), list2) for x in y]
['A', 1, 'A', 2, 'A', 3, 'A', 4]
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
How do I iterate through two lists in parallel?
(8 answers)
How do I concatenate two lists in Python?
(31 answers)
Closed 6 months ago.
I'm trying to extend two lists of lists in python, so that item 1 of the first list of lists extends with item 1 of the second list of lists, and so forth.
I'm new to this and self-taught, so could be missing something very simple, but I can't find an answer anywhere.
This is what I feel the code should be, but obviously not working.
list1[x].extend(list2[x]) for x in list1
What I'm trying to achieve is this:
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [[a,b,c],[d,e,f],[g,h,i]]
output = [[1,2,3,a,b,c],[4,5,6,d,e,f],[7,8,9,g,h,i]]
Any ideas?
You can use zip:
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [['a','b','c'], ['d','e','f'], ['g','h','i']]
output = [sub1 + sub2 for sub1, sub2 in zip(list1, list2)]
print(output)
# [[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
Use zip() to loop over the two lists in parallel. Then use extend() to append the sublist from list2 to the corresponding sublist in list1.
for l1, l2 in zip(list1, list2):
l1.extend(l2)
print(list1)
list1[x].extend(list2[x]) does achieve the result you want for one pair of lists, if list1[x] is [1, 2, 3] and list2[x] is [a, b, c], for example.
That means x has to be an index:
x = 0
list1[x].extend(list2[x])
# now the first list in `list1` looks like your desired output
To do that for every pair of lists, loop over all possible indexes x – range(upper) is the range of integers from 0 (inclusive) to upper (exclusive):
for x in range(len(list1)):
list1[x].extend(list2[x])
# now `list1` contains your output
To produce a new list of lists instead of altering the lists in list1, you can concatenate lists with the + operator instead:
>>> [1, 2, 3] + ['a', 'b', 'c']
[1, 2, 3, 'a', 'b', 'c']
output = []
for x in range(len(list1)):
output.append(list1[x] + list2[x])
And finally, the idiomatic Python for this that you’ll get around to someday is:
output = [x1 + x2 for x1, x2 in zip(list1, list2)]
I have simply used list comprehension with list addition, while zipping the correlated lists together
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [['a','b','c'], ['d','e','f'], ['g','h','i']]
list3 = [l1+l2 for l1, l2 in zip(list1, list2)]
print(list3)
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
This question already has answers here:
Transpose list of lists
(14 answers)
Closed 1 year ago.
I have 3 lists examples below, I want to match each one and create new lists
l1 = [a,b,c]
l2= [1,2,3]
l3=[7,8,9]
How do I create 3 new lists with expected output:
l1 = [a,1,7]
l2= [b,2,8]
l3=[c,3,9]
Thanks!
Use zip or a comprehension:
# Create a list of lists
ls = [l1, l2, l3]
ls = list(zip(*ls)) # output: a list of tuples
# OR
ls = [l for l in zip(*ls)] # output: a list of lists
Output:
>>> ls[0]
['a', 1, 7]
>>> ls[1]
['b', 2, 8]
>>> ls[2]
['c', 3, 9]
This question already has answers here:
How to zip two lists of lists in Python?
(5 answers)
Closed 7 years ago.
I've 2 lists of list, for example:
a = [['a','b','c'],[1,2,3]]
b = [['d','e','f'],[4,5,6]]
What I need is:
c = [['a','b','c','d','e','f'],[1,2,3,4,5,6]]
I can't figure out how to do this, any help is welcome.
Thank you a lot,
Regards
You can use zip for that:
list1 = [['a','b','c'],[1,2,3]]
list2 = [['d','e','f'],[4,5,6]]
list3 = [a + b for a, b in zip(list1, list2)]
Output of list3 would be:
[['a', 'b', 'c', 'd', 'e', 'f'], [1, 2, 3, 4, 5, 6]]
Try something like this:
map(lambda x,y:x+y,a,b)
EDIT:
This version should work for lists with different number of elements:
map(lambda x,y:(x or []) + (y or []),a,b)
a = [['a','b','c'], [1,2,3]]
b = [['d','e','f'], [4,5,6]]
S=[i+j for i,j in zip(a,b)]
This question already has answers here:
How to sort nested lists into seperate lists with unique values in python?
(2 answers)
Closed 9 years ago.
So I have a nested list that contains words and numbers like the following example:
nested_list = [['This', 1],['is' , 2],['a', 3],['list', 4]]
I also have a list of numbers:
number_list = [2,3]
I want to generate a two nested lists based on weather the second element of the list contains a number in the list of numbers.
I want to output to be:
list1 = [['is', 2],['a', 3]] #list one has values that matched the number_list
list2 = [['This', 1],['list', 4]] #list two has values that didn't match the number_list
I was using a for loop to iterate through the list but I was hoping that there was a better way.
Using two list comprehensions:
>>> nested_list = [['This', 1],['is' , 2],['a', 3],['list', 4]]
>>> number_list = [2,3]
>>> list1 = [item for item in nested_list if item[1] in number_list]
>>> list2 = [item for item in nested_list if item[1] not in number_list]
>>> list1
[['is', 2], ['a', 3]]
>>> list2
[['This', 1], ['list', 4]]
Using a dict( only single iteration is required):
>>> dic = {'list1':[], 'list2':[]}
for item in nested_list:
if item[1] in number_list:
dic['list1'].append(item)
else:
dic['list2'].append(item)
...
>>> dic['list1']
[['is', 2], ['a', 3]]
>>> dic['list2']
[['This', 1], ['list', 4]]
If number_list is huge then convert it to a set first to improve efficiency.
You can use filter:
In [11]: filter(lambda x: x[1] in number_list, nested_list)
Out[11]: [['is', 2], ['a', 3], ['list', 3]]
In [12]: filter(lambda x: x[1] not in number_list, nested_list)
Out[12]: [['This', 1]]