Pairwise appending in python without ZIP [duplicate] - python

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 7 years ago.
I am currently learning list comprehension in Python. How would I do the following:
l1 = [2,4,6,8]
l2 = [2,3,4,5]
l = [*some list comprehension*]
so that
l = [[2,2],[4,3],[6,4],[8,5]]
EDIT: Can I do this without zip?

You want the zip function.
Example -
>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>>
>>> l = list(zip(l1,l2))
>>> l
[(2, 2), (4, 3), (6, 4), (8, 5)]
If you want the inner lists to be of type list instead of tuple -
>>> l = [list(x) for x in zip(l1,l2)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]
In python 3.x, zip returns an iterator, so if you do not want a list, but just want to iterate over each combined (zipped) element, you can just directly use - zip(l1,l2) .
As it is asked in the question, to do it without zip function, you can use enumerate function to get the index as well as the element from one list and then use the index to get the element from second list.
>>> l = [[x,l2[i]] for i,x in enumerate(l1)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]
But this would not work unless both lists have same size.Also not sure why you would want to do it without zip .

Using list comprehension and zip:
>>> l1 = [2, 4, 6, 8]
>>> l2 = [2, 3, 4, 5]
>>> [[x, y] for x, y in zip(l1, l2)]
[[2, 2], [4, 3], [6, 4], [8, 5]]

You can use zip as
>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>> zip(l1,l2)
[(2, 2), (4, 3), (6, 4), (8, 5)]
>>> [ list(x) for x in zip(l1,l2) ]
[[2, 2], [4, 3], [6, 4], [8, 5]]

Related

How to combine two list into one list element wise? [duplicate]

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 2 years ago.
For example, if I have:
A = [1, 2, 3]` & `B = [4, 5, 6]
and I would like to have:
C = [[1, 4], [2, 5], [3, 6]]
You can use tuple and zip to meet this requirement.
Sample code -
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = tuple(zip(a,b))
>>> print(c)
((1, 4), (2, 5), (3, 6))
There is a builtin function called zip for this:
[list(ab) for ab in zip(a,b)]
Or using map and zip:
list(map(list, zip(a,b)))
Both return:
[[1, 4], [2, 5], [3, 6]]
You can do this:
a = [1,2,3]
b = [4,5,6]
c = [list(x) for x in zip(a, b)]
In [110]: A = [1,2,3]
In [111]: B = [4,5,6]
In [112]: list(zip(A,B))
Out[112]: [(1, 4), (2, 5), (3, 6)]

Python convert Lists to List of Lists

How can I convert multiple lists such as :
[3,4,5] and [A, B, C] and [X, Y, Z]
and convert it into
[[3,A,X], [4,B,Y], [5,C,Z]]
It could be done using a for loop but is there a faster way ?
You can use zip. https://docs.python.org/3/library/functions.html#zip
Example:
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
list(zip(a,b,c))
# [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
You can use zip:
list(zip(list1, list2, list3))
i think this will work
>>> a,b,c = [1,2,3],[4,5,6],[7,8,9]
>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> c
[7, 8, 9]
>>> d= [[a[i],b[i],c[i]] for i in range(len(a))]
>>> d
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Python Mapping Arrays

I have one array pat=[1,2,3,4,5,6,7] and a second array count=[5,6,7,8,9,10,11]. Is there a way without using dictionaries to get the following array newarray=[[1,5],[2,6],[3,7],[4,8],[5,9],[6,10],[7,11]]?
You can just zip the lists
>>> pat=[1,2,3,4,5,6,7]
>>> count=[5,6,7,8,9,10,11]
>>> list(zip(pat,count))
[(1, 5), (2, 6), (3, 7), (4, 8), (5, 9), (6, 10), (7, 11)]
Or if you want lists instead of tuples
>>> [[i,j] for i,j in zip(pat,count)]
[[1, 5], [2, 6], [3, 7], [4, 8], [5, 9], [6, 10], [7, 11]]
If you want inner elements to be list, you can use -
>>> pat=[1,2,3,4,5,6,7]
>>> count=[5,6,7,8,9,10,11]
>>> newarray = list(map(list,zip(pat,count)))
>>> newarray
[[1, 5], [2, 6], [3, 7], [4, 8], [5, 9], [6, 10], [7, 11]]
This first zips the two lists, combining the ith element of each list, then converts them into lists using map function, and later converts the complete outer map object (that we get from map function) into list
Without using zip, you can do the following:
def map_lists(l1, l2):
merged_list = []
for i in range(len(l1)):
merged_list.append([l1[i], l2[i]])
return merged_list
Or, the equivalent, using a list comprehension instead:
def map_lists(l1, l2):
return [[l1[i], l2[i]] for i in range(len(l1))]

accessing all sub elements in list of list for every iteration [duplicate]

This question already has answers here:
Transpose nested list in python
(4 answers)
Closed 8 years ago.
I Have list of list, I want to loop through the list and for every iteration i want to access the ith subelements of the list.
eg:
a = [[1, 3, 4], [2, 4, 4], [3, 7, 5]]
i want to do something like this
for i in range(len(a)):
x=a[i]
for 1st iteration I want to access the 0th element from all the sub list (i.e) 1,2,3
for 2nd iteration I want to access the 1st element from all the sub list (i.e) 3,4,7
I tried several approaches but failed, is there any trick to do that
You can use zip
s = [[1, 3, 4], [2, 4, 4], [3, 7, 5]]
print zip(*s)
#[(1, 2, 3), (3, 4, 7), (4, 4, 5)]
def get_column(n, table):
result = []
for line in table:
result.append(line[n])
return result
test = [[1,2,3],[4,5,6],[7,8,9]]
for i in range(len(test[0])):
print(get_column(i, test))
Execution :
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Alternative for the zip method,
>>> a = [[1, 3, 4], [2, 4, 4], [3, 7, 5]]
>>> new_list = []
>>> for k,v in enumerate(a):
... new_list.append([])
... for item in a:
... new_list[-1].append(item[k])
...
>>> new_list
[[1, 2, 3], [3, 4, 7], [4, 4, 5]]
As mentioned in above answer you can use an elegant way with zip but if you want to access to the columns in every iteration and dont want to get all of them in one time itertools.izip is what you are looking for , itertools.izip return a generator that you can get the result in every iteration :
>>> from itertools import izip
>>> for i in izip(*a):
... print i
...
(1, 2, 3)
(3, 4, 7)
(4, 4, 5)
Also you can use pop in a for loop (less performance than izip):
>>> a = [[1, 3, 4], [2, 4, 4], [3, 7, 5]]
>>> test=[]
>>> for i in range(len(a[0])):
... for j in a:
... test.append(j.pop(0))
... print test
... test=[]
...
[1, 2, 3]
[3, 4, 7]
[4, 4, 5]

How do I transpose a List? [duplicate]

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 8 years ago.
Let's say I have a SINGLE list [[1,2,3],[4,5,6]]
How do I transpose them so they will be: [[1, 4], [2, 5], [3, 6]]?
Do I have to use the zip function? Is the zip function the easiest way?
def m_transpose(m):
trans = zip(m)
return trans
Using zip and *splat is the easiest way in pure Python.
>>> list_ = [[1,2,3],[4,5,6]]
>>> zip(*list_)
[(1, 4), (2, 5), (3, 6)]
Note that you get tuples inside instead of lists. If you need the lists, use map(list, zip(*l)).
If you're open to using numpy instead of a list of lists, then using the .T attribute is even easier:
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> print(*a)
[1 2 3] [4 5 6]
>>> print(*a.T)
[1 4] [2 5] [3 6]
The exact way of use zip() and get what you want is:
>>> l = [[1,2,3],[4,5,6]]
>>> [list(x) for x in zip(*l)]
>>> [[1, 4], [2, 5], [3, 6]]
This code use list keyword for casting the tuples returned by zip into lists.
You can use map with None as the first parameter:
>>> li=[[1,2,3],[4,5,6]]
>>> map(None, *li)
[(1, 4), (2, 5), (3, 6)]
Unlike zip it works on uneven lists:
>>> li=[[1,2,3],[4,5,6,7]]
>>> map(None, *li)
[(1, 4), (2, 5), (3, 6), (None, 7)]
>>> zip(*li)
[(1, 4), (2, 5), (3, 6)]
# ^^ 7 missing...
Then call map again with list as the first parameter if you want the sub elements to be lists instead of tuples:
>>> map(list, map(None, *li))
[[1, 4], [2, 5], [3, 6]]
(Note: the use of map with None to transpose a matrix is not supported in Python 3.x. Use zip_longest from itertools to get the same functionality...)
zip() doesn't seem to do what you wanted, using zip() you get a list of tuples. This should work though:
>>> new_list = []
>>> old_list = [[1,2,3],[4,5,6]]
>>> for index in range(len(old_list[0])):
... new_list.append([old_list[0][index], old_list[1][index]])
...
>>> new_list
[[1, 4], [2, 5], [3, 6]]

Categories

Resources