Home Coding Functions – zip()

Functions – zip()

0
Functions – zip()

# Deep in lists
#
# What can we do with lists? Let's play with it.
# Today we try the zip() function.

# Let's create some lists 
list1 = ["a", "b", "c"]
list2 = [1,2,3]
list3 = [["a",1],["b",2],["c",3]]

out1 = zip(list1,list2) # zip togheter list1 and list2
out2 = zip(*list3) #unzip list3 in two lists

# copy www.AEC.codes
OUT = out1, out2

Iterate over several iterables in parallel, producing tuples with an item from each one.
zip() returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables.
By default, zip() stops when the shortest iterable is exhausted. It will ignore the remaining items in the longer iterables, cutting off the result to the length of the shortest iterable.
zip() in conjunction with the * operator can be used to unzip a list.