How To Use zip() in Python

zip() is a Python built in that lets you combine two or more iterable objects like lists into groups of tuples. zip() is a convenient way of grouping data in multiple lists together in an elementwise fashion. *Note: Longitude is misspelled in the video! I corrected this in the code below.* Code used in the video: # Use zip() to combine iterables like lists into tuples (elementwise) # Useful for making separate lists into tuples latitude = [4,5,6,4,6,2,4,5,6] longitude = [6,3,6,3,4,5,6,8,5] [*zip(latitude, longitude)] # Can operate on more than 2 inputs altitude = [12,41,15,16,15,23,14,51,61] [*zip(latitude, longitude, altitude)] # Zip will only continue up to the length of the shortest input short = [1,2,3,4] long = [1,2,3,4,5,6,7,8] [*zip(short, long)] # If you want to keep all items, use from itertools import zip_longest short = [1,2,3,4] long = [1,2,3,4,5,6,7,8] [*zip_longest(short, long, fillvalue=None)] * Note: YouTube does not allow greater than or less than symbols i
Back to Top