I want to sort a list so that I can feed in an attribute of the list into a loop. Let me elaborate :
Let's say I have:
data = [('turtle', 'sock'), ('frog', 'hat'), ('turtle', 'shirt'), ('frog', 'boot')]
and I only want the tuples that are related to the 'turtle', so I want:
newdata = [('turtle', 'sock'), ('turtle', 'shirt')]
Then I want to put 'sock' and 'shirt' in a list so that I can do something like:
lst = ['sock', 'shirt']
Then I can do something like
print "what is the turtle wearing?"
for item in lst:
print "the turtle is wearing a " + item
My idea was to sort the array so that all things related to the turtle could be put into a separate list. Then split that, but my knowledge of list is minimal. So any help or forwarding to helpful links is greatly appreciated. I hope this is a nice basic example to express my needs.
sorted(list(map(operator.itemgetter(1), filter(lambda x: x[0] == 'turtle', data)))). Oh yeah, don't forget to import operatoritem -> wearingdict as @FatalError suggested.