How To Use zip() in Python

How To Use zip() in Python

Channel:
Subscribers:
42,900
Published on ● Video Link: https://www.youtube.com/watch?v=kG6nhCinNus



Category:
Guide
Duration: 4:04
4,402 views
193


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 itertools.zip_longest
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 in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones.




Tags:
zip()
zip in python
zip() in python
python zip
python zip function
python zip()
python built ins
python basics
python combine lists
python elementwise matching
python tuples from lists
zip python
how to use zip() in python
hot to use zip
how to use zip()
python built in functions
python standard library