This question already has answers here:
Combine two lists into one multidimensional list
(4 answers)
Closed 5 years ago.
I have 2 lists with the same size. I want to make a new list which the combination of both of them (list of lists) in a way that elements with the same indexes would be in a list and this list would have the same index.
input example:
a = [1, 2, 3]
b = [4, 5, 6]
combined = [[1, 4], [2, 5], [3, 6]]
do you know how to do that?
Using the built-in zip:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> zip(a, b)
[(1, 4), (2, 5), (3, 6)]
This gives you the list.
combined = [[i,k] for i,k in zip(a,b)]
this will give you list of tuples
combined = list(zip(a, b))
if you really need your elements to be lists then we can write
combined = list(map(list, zip(a, b)))
Use zip
>>> list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
Or you want list instead of tuples :
>>> [[x,y] for x,y in zip(a,b)]
[[1, 4], [2, 5], [3, 6]]
You can zip them:
list(zip(a, b))
a = [1, 2, 3]
b = [4, 5, 6]
combined = list(zip(a,b))
for i in combined:
print(i)
Use zip command to combine both the list.
Related
This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 4 years ago.
I am struggling to combine the values of two arrays in Python. I want to get the values of two arrays as pairs.
EXAMPLE: Suppose we have two arrays a and b as below:
a = [[1, 2, 3], [4, 5, 6], [0, 3, 1]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print()
b = [[4, 0, 3], [6, 3, 6], [1, 4, 1]]
for i in range(len(b)):
for j in range(len(b[i])):
print(b[i][j], end=' ')
print()
How can I combine the values of two arrays as pairs similar to:
array([[(1,4), (2,0), (3,3)],
[(4,6), (5,3), (6,6)],
[(0,1), (3,4), (1,1)]])
I am not sure if it can be done as an array or a list.
You can combine list comprehension and zip() to do that:
a = [[1, 2, 3], [4, 5, 6], [0, 3, 1]]
b = [[4, 0, 3], [6, 3, 6], [1, 4, 1]]
c = [ list(zip(a[x],b[x])) for x in range(len(a))] # works b/c len(a) = len(b)
print(c)
Output
[[(1, 4), (2, 0), (3, 3)], [(4, 6), (5, 3), (6, 6)], [(0, 1), (3, 4), (1, 1)]]
This works correctly as a and b have the same amount of inner lists and the inner lists have the same length. zip() does only create tuples for 2 lists up to the shorter length of both. For inequeal length lists you can use itertools.zip_longest(...) which uses None or a specified default to fill up the shorter list.
This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Combining lists into one [duplicate]
(8 answers)
Closed 6 years ago.
So I have something like :
l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
Expected output is : ls=[[1,4,7],[2,5,8],[3,6,9]]
What it will be the most corect way to do that?
Use zip and then list comprehension to turn the tuples into lists
[list(x) for x in zip(l1, l2, l3)]
Result:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> l1=[1,2,3]
>>> l2=[4,5,6]
>>> l3=[7,8,9]
>>> zip(l1, l2, l3)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
The built-in function zip will help you with what you want.
zip the three lists:
>>> l1 = [1,2,3]
>>> l2 = [4,5,6]
>>> l3 = [7,8,9]
>>> zip(l1,l2,l3)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Use a list comprehension to cast the tuples into lists to have a list of lists:
>>> [list(i) for i in zip(l1,l2,l3)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
This question already has answers here:
Equally distribute a list in python
(2 answers)
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
I have objects list:
l = [1, 2, 3, 4, 5, 6]
i find some snippet.. http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/
but they split like this:
[1, 2] [3, 4] [5, 6]
i need split list like this:
l1 = [1, 4]
l2 = [2, 5]
l3 = [3, 6]
Please, help build right templatetag.
You could gather your lists l1, l2 l3 in another list using a list comprehension, and afterwards do something to them. For example:
l = [1, 2, 3, 4, 5, 6]
x = [[l[i]] + [l[i+3]] for i in range(len(l) - 3)]
for a in x:
print(a)
will get you
[1, 4]
[2, 5]
[3, 6]
If you know x contains three lists, you can assign l1, l2, l3 with
l1, l2, l3 = x
Of course, you could just manually assign l1, l2, l3 too.
l1 = [l[0]] + [l[3]]
...
h = int(len(l)/2)
l1, l2, l3 = zip( l[:h], l[h:] )
l[:h] is the first half and l[h:] the second half. See list slices.
>>> l[:h], l[h:]
([1, 2, 3], [4, 5, 6])
Then the zip function, see zip.
>>> zip([1, 2, 3], [4, 5, 6])
[(1, 4), (2, 5), (3, 6)]
I am trying to create a function that can do this.
>>> rearrange_list([1,2,3],[4,5,6])
[[1,4],[2,5],[3,6]]
So far what I have is
def rearrange_list(my_list):
i = 0
n = 0
new_list = []
for i in range(0, len(my_list[n])):
for n in range(0,len(my_list)):
new_list += [my_list[n][i]]
print(new_list)
n += 1
return new_list
but this code returns [1, 4, 2, 5, 3, 6], a single list instead of
a list of lists like I want it to and I can't seem to figure out how to make the
function output a list of lists based on the index of the list inside the list.
Use zip like so:
zip(list_a, list_b)
You can use zip and a list comprehension:
>>> def rearrange_lists(a, b):
... return [list(x) for x in zip(a, b)]
...
>>> rearrange_lists([1,2,3], [4,5,6])
[[1, 4], [2, 5], [3, 6]]
>>>
Note that the above function only handles two lists. If you want to handle any number of lists, you can use this:
>>> def rearrange_lists(*lsts):
... return [list(x) for x in zip(*lsts)]
...
>>> rearrange_lists([1,2,3], [4,5,6], [7,8,9])
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>>
Use zip() instead:
>>> zip([1,2,3], [4,5,6])
[(1, 4), (2, 5), (3, 6)]
or, for your specific varargs version:
>>> lsts = ([1,2,3], [4,5,6])
>>> zip(*lsts)
[(1, 4), (2, 5), (3, 6)]
or map the results to lists if tuples won't do:
map(list, zip([1,2,3], [4,5,6])) # Python 2
[list(t) for t in zip([1,2,3], [4,5,6])] # Python 2 and 3
As a replacement for your function:
def rearrange_list(*my_lists):
return [list(t) for t in zip(*my_lists)]
I have a list of N lists, each sub-list containing M elements.
How do I create a list of M lists with N elements each, containing the n-th elements from the initial lists?
Let's say I have this
myList = [[1, 2, 3],[4, 5, 6]]
I want to have
[[1, 4],[2, 5],[3, 6]]
Performance is also important. I have sublists of millions of elements (though, one dimension will always be small: like 1.000.000 x 8 )
This will give tuples, but it's trivial to extend this to contain lists:
zip(*myList)
i.e.
[list(i) for i in zip(*myList)]
Use
myList = [[1, 2, 3],[4, 5, 6]]
zip(*myList )
>>> lst = [[1, 2, 3], [4, 5, 6]]
>>> zip(*lst)
[(1, 4), (2, 5), (3, 6)]
zip returns a list in Python 2, but an iterator in Python 3, which you have to convert to a list if you need to (list(zip(lst)))
See also: Matrix Transpose in Python, Transpose/Unzip Function (inverse of zip)?
What zip(*lst) does is unpacks the elements of lst using the * operator into separate arguments of the zip function.
We know what happens when we put two lists in:
>>> zip([1, 2, 3], [4, 5, 6])
[(1, 4), (2, 5), (3, 6)]
So if we have a list containing those two lists, the * will unpack the list to form two separate arguments - the equivalent of the above call.
>>> zip(*[[1, 2, 3], [4, 5, 6]])
[(1, 4), (2, 5), (3, 6)]
Maybe with the help of itertools.izip()
itertools.izip(*myList)
To create lists from this iterator do the following:
map(list, itertools.izip(*myList))
Since you mentioned efficiency, I tried these with PyPy and somewhere around 100x1000 they start to be better than zip (on CPython however they are worse):
myList = [[1, 2, 3],[4, 5, 6]]
newList = [[] for _ in xrange(len(myList[0]))]
for innerList in myList:
for elIdx in xrange(len(innerList)):
newList[elIdx].append(innerList[elIdx])
print newList
And my first attempt (which is worse), but still better then zip on PyPy:
newList = [[None for i in xrange(len(myList))] for j in xrange(len(myList[0]))]
for i in xrange(len(myList)):
for j in xrange(len(myList[i])):
newList[j][i] = myList[i][j]
print newList