Skip to main content
deleted 32 characters in body
Source Link
etoricky
  • 699
  • 1
  • 8
  • 10

Given a function that has 3 argumentsitems as argument

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbersitems

sum([1,2,3]) # error, needs 3 argumentsitems, not 1 list

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, 1 list becomes 3 argumentsitems

Imagine this toy with a bag of a triangle, a circle and a rectangle item. That bag does not directly fit. You need to unpack the bag to take those 3 items and now they fit. The Python * operator does this unpack process.

enter image description here

Given a function that has 3 arguments

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbers

sum([1,2,3]) # error, needs 3 arguments, not 1 list

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, 1 list becomes 3 arguments

Given a function that has 3 items as argument

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 items

sum([1,2,3]) # error, needs 3 items, not 1 list

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, 1 list becomes 3 items

Imagine this toy with a bag of a triangle, a circle and a rectangle item. That bag does not directly fit. You need to unpack the bag to take those 3 items and now they fit. The Python * operator does this unpack process.

enter image description here

deleted 32 characters in body
Source Link
etoricky
  • 699
  • 1
  • 8
  • 10

Given a function that has 3 arguments

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbers

sum([1,2,3]) # error, cannot assign 1 list toneeds a3 functionarguments, withnot 31 argumentslist

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, same as above, simpler, * turns this1 list to x=1,becomes y=2,3 z=3arguments

Given a function that has 3 arguments

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbers

sum([1,2,3]) # error, cannot assign 1 list to a function with 3 arguments

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, same as above, simpler, * turns this list to x=1, y=2, z=3

Given a function that has 3 arguments

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbers

sum([1,2,3]) # error, needs 3 arguments, not 1 list

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, 1 list becomes 3 arguments
Source Link
etoricky
  • 699
  • 1
  • 8
  • 10

Given a function that has 3 arguments

sum = lambda x, y, z: x + y + z
sum(1,2,3) # sum 3 numbers

sum([1,2,3]) # error, cannot assign 1 list to a function with 3 arguments

x = [1,2,3][0]
y = [1,2,3][1]
z = [1,2,3][2]
sum(x,y,z) # ok

sum(*[1,2,3]) # ok, same as above, simpler, * turns this list to x=1, y=2, z=3