So I have a script like this
for key, Value in mydictionary.iteritems():
if 'Mammal' in Value[1]:
#because the value is a list of 2 items and I want to get at the second one
Value[1] = Value[1].strip('Mammal')
This code effectively removes Mammal from the beginning of the second item in the Value list. Now I want to make this nicer python looking with list comprehension so I came up with this but obviously is wrong…. Any help?
Value[1] = [Value[1].strip('Mammal') for Key, Value in mydictionary.iteritems() if 'Mammal' in Value[1] ]
Also, on the same lines, a list comprehension to list all the keys in this dictionary. I am having a hard time coming up with that one.
I came up with:
for key, Value in mydictionary.iteritems():
Templist.append(key)
but as a list comprehension I am thinking….but it doesn’t work 🙁
alist = [key for Key, Value in mydictionary.iteritems()]
Output:
Although I wouldn’t call this objectively “nicer looking”, so the iterative method may be preferable.