[Tutor] selecting elements from dictionary
Knacktus
knacktus at googlemail.com
Wed Sep 15 05:51:48 CEST 2010
> xdic
> {11135457: [1], 11135492: [1], 11135913: [1], 11135436: [1, 2],
> 11135699: [1, 2], 11135702: [1, 3], 11135901: [1]}
>
>
> I want to print only those items that have [1,2] and [1,3] in any order,
> such as [1,2] or [2,1], [3,1] or [1,3]
>
>
> >>> for item in xdic.keys():
> ... if [1,2] in xdic[item]:
> ... print item
>
You can loop over the values directly:
xdic = {11135457: [1], 11135492: [1], 11135913: [1], 11135436: [1, 2],
11135699: [1, 2], 11135702: [1, 3], 11135901: [1]}
for values in xdic.values():
if len(values) == 2:
print values
or if you only want values which contain 1 and 2 or 3:
for values in xdic.values():
if 1 in values and 2 in values or 3 in values:
print values
or a combination of the above, where you check the length and the
content of the list.
HTH,
Jan
More information about the Tutor
mailing list