28 April, 2010

Being Pythonic

Tuple is a very important data structure in python. You can directly assign tuple elements to variables like this.

Tuple assignment
tup = (3,'xyz',[1,2,3])
ind, myKey, myList = tup 
This is called unpacking. It is wise to do this only on tuples since they are immutable and it is sure that the number of elements being unpacked is same as the number of variables on the left side. This can be very useful when using *args and **kwargs. More on that later.

Use negative index
myList[-1] returns the last element

No comments:

Post a Comment