How to merge lists into a list of tuples? - python

What is the Pythonic approach to achieve the following?
# Original lists:
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
# List of tuples from 'list_a' and 'list_b':
list_c = [(1,5), (2,6), (3,7), (4,8)]
Each member of list_c is a tuple, whose first member is from list_a and the second is from list_b.

In Python 2:
>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> zip(list_a, list_b)
[(1, 5), (2, 6), (3, 7), (4, 8)]
In Python 3:
>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> list(zip(list_a, list_b))
[(1, 5), (2, 6), (3, 7), (4, 8)]

In python 3.0 zip returns a zip object. You can get a list out of it by calling list(zip(a, b)).

You can use map lambda
a = [2,3,4]
b = [5,6,7]
c = map(lambda x,y:(x,y),a,b)
This will also work if there lengths of original lists do not match

Youre looking for the builtin function zip.

I am not sure if this a pythonic way or not but this seems simple if both lists have the same number of elements :
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
list_c=[(list_a[i],list_b[i]) for i in range(0,len(list_a))]

The output which you showed in problem statement is not the tuple but list
list_c = [(1,5), (2,6), (3,7), (4,8)]
check for
type(list_c)
considering you want the result as tuple out of list_a and list_b, do
tuple(zip(list_a,list_b))

I know this is an old question and was already answered, but for some reason, I still wanna post this alternative solution. I know it's easy to just find out which built-in function does the "magic" you need, but it doesn't hurt to know you can do it by yourself.
>>> list_1 = ['Ace', 'King']
>>> list_2 = ['Spades', 'Clubs', 'Diamonds']
>>> deck = []
>>> for i in range(max((len(list_1),len(list_2)))):
while True:
try:
card = (list_1[i],list_2[i])
except IndexError:
if len(list_1)>len(list_2):
list_2.append('')
card = (list_1[i],list_2[i])
elif len(list_1)<len(list_2):
list_1.append('')
card = (list_1[i], list_2[i])
continue
deck.append(card)
break
>>>
>>> #and the result should be:
>>> print deck
>>> [('Ace', 'Spades'), ('King', 'Clubs'), ('', 'Diamonds')]

Or map with unpacking:
>>> list(map(lambda *x: x, list_a, list_b))
[(1, 5), (2, 6), (3, 7), (4, 8)]
>>>

One alternative without using zip:
list_c = [(p1, p2) for idx1, p1 in enumerate(list_a) for idx2, p2 in enumerate(list_b) if idx1==idx2]
In case one wants to get not only tuples 1st with 1st, 2nd with 2nd... but all possible combinations of the 2 lists, that would be done with
list_d = [(p1, p2) for p1 in list_a for p2 in list_b]

Like me, if anyone needs to convert it to list of lists (2D lists) instead of list of tuples, then you could do the following:
list(map(list, list(zip(list_a, list_b))))
It should return a 2D List as follows:
[[1, 5],
[2, 6],
[3, 7],
[4, 8]]

Related

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]]

How to convert a list to a list of tuples?

I am newbie to Python and need to convert a list to dictionary. I know that we can convert a list of tuples to a dictionary.
This is the input list:
L = [1,term1, 3, term2, x, term3,... z, termN]
and I want to convert this list to a list of tuples (or directly to a dictionary) like this:
[(1, term1), (3, term2), (x, term3), ...(z, termN)]
How can we do that easily in Python?
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
# Create an iterator
>>> it = iter(L)
# zip the iterator with itself
>>> zip(it, it)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
You want to group three items at a time?
>>> zip(it, it, it)
You want to group N items at a time?
# Create N copies of the same iterator
it = [iter(L)] * N
# Unpack the copies of the iterator, and pass them as parameters to zip
>>> zip(*it)
Try with the group clustering idiom:
zip(*[iter(L)]*2)
From https://docs.python.org/2/library/functions.html:
The left-to-right evaluation order of the iterables is guaranteed.
This makes possible an idiom for clustering a data series into
n-length groups using zip(*[iter(s)]*n).
List directly into a dictionary using zip to pair consecutive even and odd elements:
m = [ 1, 2, 3, 4, 5, 6, 7, 8 ]
d = { x : y for x, y in zip(m[::2], m[1::2]) }
or, since you are familiar with the tuple -> dict direction:
d = dict(t for t in zip(m[::2], m[1::2]))
even:
d = dict(zip(m[::2], m[1::2]))
Using slicing?
L = [1, "term1", 2, "term2", 3, "term3"]
L = zip(L[::2], L[1::2])
print L
Try this ,
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> it = iter(L)
>>> [(x, next(it)) for x in it ]
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
>>>
OR
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> [i for i in zip(*[iter(L)]*2)]
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
OR
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> map(None,*[iter(L)]*2)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
>>>
[(L[i], L[i+1]) for i in xrange(0, len(L), 2)]
The below code will take care of both even and odd sized list :
[set(L[i:i+2]) for i in range(0, len(L),2)]

Python: How can I obtain something in a list of list?

Currently I have the following list:
>>> a = list()
>>> b = [1, 2, 3]
>>> c = [4,5,6]
>>> d = [7, 8]
>>> a.append(b)
>>> a.append(c)
>>> a.append(d)
>>> del b, c, d
>>> print a
[[1, 2, 3], [4, 5, 6], [7, 8]]
I want to obtain the first item on b, c and d (value 1, 4 and 7). How could I do that in one line of code?
I want to obtain the remaining item on b, c and d (value 2, 3, 5, 6 and 8). How could I do that in one line of code?
>>> zip(*a)[0]
(1, 4)
The zip(*a) is like a transpose function.
>>> zip(*a)
[(1, 4), (2, 5), (3, 6)]
The [0] indexes the first element.
>>> zip(*a)[0]
(1, 4)
The second problem is trickier - zip won't do the trick. Instead you need a list comprehension.
>>> [i for l in a for i in l[1:]]
[2, 3, 5, 6, 8]
Or if you don't need a flattened list, simply:
>>> [l[1:] for l in a]
[[2, 3], [5, 6], [8]]
This should do the trick:
[lst[0] for lst in a]
It iterates over a and for every element (which is a list) it collects the first value.
There are plenty of ways to do this. This would be one:
>>> print [x[0] for x in a]
[1,4]
Or putting print inside the for loop:
>>> for x in a: print x[0],
1 4
The most basic way if you only want the first elements of the first two sublists:
>>> print a[0][0], a[1][0]
1 4
I'd go with niklas B. answer zip(b,c)[0], but, to give you more flexibility, if you don't know the number of sublists in the list, you can also try:
[el[0] for el in a]
and gives you all the first items in the sublists of a.

Iterating through nested list

How do i create a function to iterate my list in this manner.
Seems simply but im stuck...
myList= [[1,2,3], [4,5,6], [7,8,9]]
def name(myList):
somework..
newList = [[1,4,7]. [ 2,5,8], [3,6,9]]
In [3]: zip(*myList)
Out[3]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
if you specifically want list
In [4]: [list(x) for x in zip(*myList)]
Out[4]: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
for more details on zip function look at this
zip is what you want + argument unpacking. It's awesome. I like to think of it as python's builtin transpose.
newList = zip(*myList)
This will actually give you an iterable (python3.x) or list (python2.x) of tuple, but that's good enough for most purposes.

In Python, how can I create lists that contain a certain index of other lists?

Say I have several lists
A = [1,2,3]
B = [4,5,6]
C = [7,8,9]
How can I create new lists so that they contain matching indexes, as in:
D = [1,4,7]
E = [2,5,8]
F = [3,6,9]
The original lists will always contain the same number of elements, and I need this to work for any number of elements and any number of lists, not just three. I figure I need to loop over a range, but I'm not sure how to go about it.
If I understand you correctly, you may be looking for zip():
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = [7, 8, 9]
>>> zipped = zip(a, b, c)
>>> zipped
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
If instead of several lists, you have a list of the lists that you want to zip, then you can use * for unpacking the sublists, as follows:
>>> myListOfLists = [[1,2,3],[4,5,6],[7,8,9]]
>>> zipped = zip(*myListOfLists)
>>> zipped
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Categories

Resources