xxxxxxxxxx
# Example of list unpacking
my_list = [1, 2, 3]
a, b, c = my_list
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
xxxxxxxxxx
# unpack a list python:
list = ['red', 'blue', 'green']
# option 1
red, blue = colors
# option 2
*list
xxxxxxxxxx
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']
xxxxxxxxxx
def fun(a, b, c, d):
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
# Unpacking list into four arguments
fun(*my_list)
xxxxxxxxxx
elems = [1, 2, 3, 4]
a, b, c, d = elems
print(a, b, c, d)
# 1 2 3 4
# or
a, *new_elems, d = elems
print(a)
print(new_elems)
print(d)
# 1
# [2, 3]
# 4