wtz
wtz

Reputation: 383

Convert list to sequence of variables

I was wondering if this was possible...

I have a sequence of variables that have to be assigned to a do.something (a, b) a and b variables accordingly.

Something like this:

# # Have a list of sequenced variables.
list = 2:90 , 1:140 , 3:-40 , 4:60

# # "Template" on where to assign the variables from the list.    
do.something (a,b)
# # Assign the variables from the list in a sequence with possibility of "in between" functions like print and time.sleep() added.

do.something (2,90)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)   
do.something (1,140)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)  
do.something (3,-40)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)  
do.something (4,60)
time.sleep(1)
print "Did something (%d,%d)" % (# # vars from list?)  

Any ideas?

Upvotes: 2

Views: 11594

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65176

arglist = [(2, 90), (1, 140), (3, -40), (4, 60)]
for args in arglist:
    do.something(*args)
    time.sleep(1)
    print "Did something (%d,%d)" % args

The * means "use the values in this tuple as the arguments". Hint: you can do the same with keyword arguments by using ** and a dict.

Upvotes: 4

Related Questions