We can "unpack" values with the following
myfruit, yourfruit = ('apple', 'banana')
print(myfruit, yourfruit)
And we can use the star mark (*) for variable length:
myfruit, *otherfruits, yourfruit = ['apple', 'banana', 'orange', 'plum']
print(myfruit)
print(otherfruits)
print(type(otherfruits))
print(yourfruit)
Let's try unpacking with functions ... it's the same
def give_me_fruits():
return ['apple', 'banana', 'orange', 'plum']
*myfruits, _ = give_me_fruits()
print(myfruits)
Above we used _
to denote a return that we want to just ignore (and not bind to a name).
def some_fruits(fruit, *morefruits):
print('The best fruit is %s' % fruit)
for f in morefruits:
print('...not %s' % f)
some_fruits('apple', 'banana', 'orange', 'plum')
def average_total(a,b):
totalsum = a + b
average = totalsum/2
diff = a - b
return average,totalsum, diff
#We can unpack values returned from a function
ave,tot,diff = average_total(3,5)
print(ave, tot, diff)
#We can unpack values using the star mark (*) for variable length
ave, *othervar, diff = average_total(3,5)
print(ave, diff)
ave, *_ = average_total(3,5)
print(ave)