py unpack list

18

# unpack a list python:
list = ['red', 'blue', 'green']

# option 1
red, blue = colors

# option 2
*list
def print_items(item1, item2, item3, item4, item5, item6):
  print(item1, item2, item3, item4, item5, item6)

fruit = ["apple", "banana", "orange", "pineapple", "watermelon", "kiwi"]

# deconstructs/unpacks the "fruit" list into individual values
my_function(*fruit)
l = [0, 1, 2]

a, b, c = l

print(a)
print(b)
print(c)
# 0
# 1
# 2
colors = ["Red", "Green", "Blue"]

# Normally we do this
red = colors[0]
green = colors[1]
blue = colors[2]

# List Unpacking using python
red, green, blue = colors
print(red, green, blue) #Output: Red Green Blue

fruits = ["Apple", "Mango", "Banana", "Apricot",
"Jackfruit", "Jujube"]

apple, mango, *others = fruits
print(apple, mango, others)
# Output: Apple Mango ['Banana', 'Apricot', 'Jackfruit', 'Jujube']

Comments

Submit
0 Comments