Python - Zip (Collection)

Card Puncher Data Processing


A zip is a collection constructed from other collections all of the same length.

Zip create pairs of elements when passed two lists, and will stop at the end of the shorter list.

Each element of the zip is a tuple consisting of one element from each of the input collections.

>>> list(zip([1,2],[3,4])) # zip with two list collections
[(1, 3), (2, 4)]
>>> list(zip({1,2},{3,4})) # zip can be used also with set
[(1, 3), (2, 4)]
>>> list(zip({1,2},{3,4,5})) # the 5 will not be an element of the list
[(1, 3), (2, 4)]


Comprehension iteration on zip with unpacking. See Zip

>>> characters = ['Neo', 'Morpheus', 'Trinity']
>>> actors = ['Keanu', 'Laurence', 'Carrie-Anne']
>>> [character+' is played by '+actor for (character,actor) in zip(characters,actors)]
['Neo is played by Keanu', 'Morpheus is played by Laurence', 'Trinity is played by Carrie-Anne']

Discover More
Card Puncher Data Processing
Python - Collection

collection in Python: Data Type: Type Sequence (Ordered) Mutable Duplicate Yes Yes Yes Yes No Yes No Yes No No Yes No
Card Puncher Data Processing
Python - Comprehension (Iteration)

Comprehension are a way to loop over elements of: a , , , tuple, , or . Comprehensions permit to build collections out of other collections. Comprehensions allow one to express computations...
Card Puncher Data Processing
Python - List

Lists are used to store a collection of different pieces of information in a sequence order of under a single variable name. A list can be used to implement a . A list is mutable whereas a tuple is not....
Card Puncher Data Processing
Python - Sequence (type)

The following data structure are sequence: Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two...

Share this page:
Follow us:
Task Runner