tags:

views:

73

answers:

1

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?

+4  A: 
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.

Matti Virkkunen
Using `list` as a variable name is not kosher in python.
alex vasi
Woops, good point... let's not override built-in types. Fixed.
Matti Virkkunen
To be fair, Python doesn't care. At least, not until it hits a line where the programmer wanted to use the `list` type but forgot that he used the name for something else.
Ignacio Vazquez-Abrams
Of course it doesn't, but writing code that breaks everyone's expectations is usually not a good idea.
Matti Virkkunen
This is sweet! Perfect! Thank you guys!
I put `True, False = False, True` at the top of all my programs. It keeps future editors on their toes
Michael Mrozek