I think this is a good way for beginners. Do not name your list - there is a built-in call to list
>>> dictA = { ... 'a' : ('duck','duck','goose'), ... 'b' : ('goose','goose'), ... 'c' : ('duck','duck','duck'), ... 'd' : ('goose'), ... 'e' : ('duck','duck') ... } >>> my_list = [] >>> for key in dictA: ... if dictA[key].count('duck') > 1: ... my_list.append(key) ... >>> my_list ['a', 'c', 'e']
The next step is to use .items() , so you don't need to look up the value for each key
>>> my_list = [] >>> for key, value in dictA.items(): ... if value.count('duck') > 1: ... my_list.append(key) ... >>> my_list ['a', 'c', 'e']
When you understand this, you will realize that understanding the list in Ignacio is easier to understand.
source share