Zip lists in Python
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Zip Lists In Python
00:40 Accepted Answer Score 275
01:12 Answer 2 Score 30
01:27 Answer 3 Score 75
01:46 Answer 4 Score 43
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/1370...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27
#avk47
ACCEPTED ANSWER
Score 275
When you zip() together three lists containing 20 elements each, the result has twenty elements. Each element is a three-tuple.
See for yourself:
In [1]: a = b = c = range(20)
In [2]: zip(a, b, c)
Out[2]:
[(0, 0, 0),
(1, 1, 1),
...
(17, 17, 17),
(18, 18, 18),
(19, 19, 19)]
To find out how many elements each tuple contains, you could examine the length of the first element:
In [3]: result = zip(a, b, c)
In [4]: len(result[0])
Out[4]: 3
Of course, this won't work if the lists were empty to start with.
ANSWER 2
Score 75
zip takes a bunch of lists likes
a: a1 a2 a3 a4 a5 a6 a7...
b: b1 b2 b3 b4 b5 b6 b7...
c: c1 c2 c3 c4 c5 c6 c7...
and "zips" them into one list whose entries are 3-tuples (ai, bi, ci). Imagine drawing a zipper horizontally from left to right.
ANSWER 3
Score 43
In Python 2.7 this might have worked fine:
>>> a = b = c = range(20)
>>> zip(a, b, c)
But in Python 3.4 it should be (otherwise, the result will be something like <zip object at 0x00000256124E7DC8>):
>>> a = b = c = range(20)
>>> list(zip(a, b, c))
ANSWER 4
Score 30
zip creates a new list, filled with tuples containing elements from the iterable arguments:
>>> zip ([1,2],[3,4])
[(1,3), (2,4)]
I expect what you try to so is create a tuple where each element is a list.