How To Use zip() in Python

preview_player
Показать описание
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)]

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.
Рекомендации по теме
Комментарии
Автор

Thank you for explaining this better than my teacher!

noir
Автор

It helped me a lot!!! Thanks a million ❤

moniquebrasilbaptista
Автор

this helped me on a leetcode problem. Thanks!

elliottperez
Автор

Hi
I want to write code that adds two binary numbers using list, loop and function. I ask the user to enter binary numbers, then get individual bit using for loop and add it to bianryList1 and then enter second binary and do the same . After that I use for loops to get the numbers in the binarylist1 and backwards. When i get one element I want to pass them to addTwoNumbers function but I dont know how. I want to pass "c" and "g" to addTwo Numbers function. i want to do normal addition, from right to left Thank you in advance. Here is my code:



binaryList1 = []
binaryList2 = []

def AddTwoNumbers(number1, number2):
return number1 + number2


n1 = input(" enter now: ")

for i in map(int, n1):
binaryList1.append(i)


n2 = input(" enter now: ")

for j in map(int, n2):
binaryList2.append(j)

for c in reversed(binaryList1):
print(c)

for g in reversed(binaryList2):
print(g)

result = AddTwoNumbers(c, g)

abdirahmanabdullahi