Zip with list output instead of tuple
If you are zipping more than 2 lists (or even only 2, for that matter), a readable way would be:
[list(a) for a in zip([1,2,3], [4,5,6], [7,8,9])]
This uses a list comprehension to apply list
to each element (tuple) in the list, converting them into lists.
Get a tuple from lists with zip() in Python
Don't put []
around the variables. Now you're zipping those lists, not the contents of the variables.
Just use:
answer = tuple(zip(name, place, weather))
Why does zip() of list of lists output such?
"The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and return it."
Basically it mean that you can do sometime like that:
list1 = [1,2,3,4]
list2 = ["h", "b", "s"]
for num, char in zip(list1, list2):
print(num, char)
# output:
# 1 h
# 2 b
# 3 s
enter value: [[1,2],[2,3]]
first you zip the list: ([1, 2],), ([2, 3],)
or just make tuples to the values
if you just enter a one list:
a = zip([1,2,3,4,5,6])
print(list(a)) # [(1,), (2,), (3,), (4,), (5,), (6,)]
it make the values in the list a tuples. (, for make it a one value tuple)
and after that you make it a list: [([1, 2],), ([2, 3],)]
Any way to zip to list of lists?
You can use a comprehension:
listz = [list(i) for i in zip(listx, listy)]
or generator expression:
listz = (list(i) for i in zip(listx, listy))
Zip lists in Python
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.
Zip list of tuples with flat list
You just need parentheses:
list_a = [(1,2), (1,2), (1,2)]
list_b = [3, 3, 3]
for (a, b), c in zip(list_a, list_b):
print(a, b, c)
Result:
1 2 3
1 2 3
1 2 3
zip function help with tuples
>>> zip((1,2,3),(10,20,30),(100,200,300))
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]
>>> [sum(x) for x in zip((1,2,3),(10,20,30),(100,200,300))]
[111, 222, 333]
To do this with an arbitrarily large set of tuples:
>>> myTuples = [(1,2,3), (10,20,30), (100,200,300)]
>>> [sum(x) for x in zip(*myTuples)]
[111, 222, 333]
sidenote: in python3, note that zip returns a lazy iterable, which you can always explicitly turn into a list like any other kind of iterable: list(zip(...))
(thanks to Seganku for catching mistake in examples in an edit which was thrice rejected by other editors)
Why does a list comprehension over a zip() call return a list containing the zip object instead of a list of zip()'s return values?
The first statement is not a list comprehension, a list comprehension would give you the same result. It is just a list literal containing a zip object:
This would be a list comprehension:
[value for value in zip([1,2,3], [3,1,4])]
The above will print the same as list(zip([1, 2, 3], [3, 1, 4]))
.
In general, [something]
means: A list with one element: something
.
On the other hand, list(something)
means: Iterate over the values in something, and make a list from the result. You can see the difference for example by putting primitive objects inside it, like a number:
>>> [2]
[2]
>>> list(2)
TypeError: 'int' object is not iterable
Related Topics
Get Column Index from Column Name in Python Pandas
Is There a Multi-Dimensional Version of Arange/Linspace in Numpy
How to Find Element by Part of Its Id Name in Selenium with Python
How to Format Axis Number Format to Thousands with a Comma in Matplotlib
Better Way to Shuffle Two Numpy Arrays in Unison
Output Pyodbc Cursor Results as Python Dictionary
Import Win32API Error in Python 2.6
How to Find Which Columns Contain Any Nan Value in Pandas Dataframe
Monkey Patching a Class in Another Module in Python
How to Force/Ensure Class Attributes Are a Specific Type
Check If a Given Key Already Exists in a Dictionary and Increment It
How to Add a Custom Loglevel to Python's Logging Facility
Creating a Class Within a Function and Access a Function Defined in the Containing Function's Scope
Finding Moving Average from Data Points in Python
How to Convert a Scikit-Learn Dataset to a Pandas Dataset