Previously, we know the zip function in Python allows us to take one element from two list or tuple. It kinda acts as the zip_shortest. See below:
1 2 3 4 5 | a = (1, 2, 3, 4, 5) b = (6, 7, 8, 9, 10, 11, 12) print(list(zip(a, b))) # [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)] |
a = (1, 2, 3, 4, 5) b = (6, 7, 8, 9, 10, 11, 12) print(list(zip(a, b))) # [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
The length is determined by the shortest one. If we want to zip the longest and fill those fields (which are undetermined) by None type, we can use the zip_longest from the itertools:
1 2 3 4 5 6 7 | from itertools import zip_longest a = (1, 2, 3, 4, 5) b = (6, 7, 8, 9, 10, 11, 12) print(list(zip_longest(a, b))) # [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10), (None, 11), (None, 12)] |
from itertools import zip_longest a = (1, 2, 3, 4, 5) b = (6, 7, 8, 9, 10, 11, 12) print(list(zip_longest(a, b))) # [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10), (None, 11), (None, 12)]
You can implement this function: Teaching Kids Programming – Introduction and Re-implement the zip and zip_longest Function in Python
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
194 wordsloading...
Last Post: Count the Unique AB Strings by Permutation
Next Post: Teaching Kids Programming - Introduction and Re-implement the zip and zip_longest Function in Python