First of all, note that in your example you are not using a list of tuples but a list of list. tuples are defined with normal brackets
("I", "am", "a", "tuple")
Sequence access
In Python, list
and tuple
are sequences, so they can be accessed with numeric indices
For lists:
item = ["Shawn", 501]
, then you can do item[0]
and item[1]
.
For tuples, it's exactly the same:
item = ("Shawn", 501)
, then you can do item[0]
and item[1]
.
Unpacking
When your tuples are small, 'opening' them - unpacking
being the Python term - is also very handy
item = ("Shawn", 501)
name, number = item # We unpack the content of the tuple into some variables
Named tuples
Python 2.6 has introduced a namedtuple() type which you may find useful. With your example, you could do something like the following:
Employee = namedtuple("Employee", ['name', 'number'])
employeeList = [
Employee("Shawn", 500),
Employee("David", 501),
Employee("Ted", 14],
Employee("Jerry", 22)
]
for item in employeeList:
if item.name == name and item.number >= 500:
print "This person is a manager."
else:
print "This person is not a manager."