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.
Answer from Matti Virkkunen on Stack OverflowHow can I convert between different sequence types in Python?
What are the three types of sequences in Python?
When should I use a list versus a tuple in Python?
Similar to list comprehension, but a bit different.
This is the map function.
lst = ["Red", "Green", "Blue"]
new_lst = map(lambda x: (x.upper(), x, ""), lst)
It basically changes every element of a list one-by-one according to the function you enter as the first parameter. Which is in this case:
lambda x: (x.upper(), x, "")
If you don't have an idea what lambda is, it is almost like in the high school math:
f(x) = (x.upper(), x, "") # basically defines a function in one line.
The code in the return statement is called list comprehension.
def list_to_items(items):
return [(i.upper(), i, "") for i in items]
You can find more info http://www.secnetix.de/olli/Python/list_comprehensions.hawk
If you are passing A as an argument to a function fn(A1, A2, A3), you can pass the elements of A individually as follows:
fn( *A )
The * operation on a list/tuple when passed to a function unpacks the elements in the list/tuple.
Assuming you want a string representation:
B = ", ".join(repr(t) for t in A)