0

I have a dictionary in the following format:

{ 'a' : [1], 'b' : [1,2,3], 'c' : [1,1,2], 'd' : [2,3,4] }

and I want to create a list of the keys which have a '1' in their values.

So my result list should look like:

['a','b','c','c']

I cannot understand how to work with duplicate values. Any ideas how can I get such a list?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Luke
  • 79
  • 1
  • 8
  • Loop through the list in the property. For each time you see `1`, append the property name to the result. – Barmar Nov 25 '15 at 21:34

5 Answers5

5

You can use list comprehensions

>>> d = { 'a' : [1], 'b' : [1,2,3], 'c' : [1,1,2], 'd' : [2,3,4] }
>>> [key for key, values in d.items() for element in values if element==1]
['c', 'c', 'b', 'a']

Here we have two nested for loops in our list comprehension. The first iterate over each key, values pairs in the dictionary and the second loop iterate over each element in the "value" list and return the key each time that element equal to 1. The result list is unordered because dict are unordered which means there are no guarantees about the order of the items.

styvane
  • 59,869
  • 19
  • 150
  • 156
3

Here is one way:

>>> x = { 'a' : [1], 'b' : [1,2,3], 'c' : [1,1,2], 'd' : [2,3,4] }
>>> list(itertools.chain.from_iterable([k]*v.count(1) for k, v in x.iteritems() if 1 in v))
['a', 'c', 'c', 'b']

If using Python 3, use items instead of iteritems.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
2

This uses two loops, k,v in d.items() which gets each (key,value) pair from the dictionary, and n in v which loops through each value in v:

d = { 'a' : [1], 'b' : [1,2,3], 'c' : [1,1,2], 'd' : [2,3,4] }
l = []
for k,v in d.items():
    for n in v:
        if n == 1:
            l.append(k)
l.sort()

If you want a one-liner:

l = sorted(k for k,v in d.items() for n in v if n == 1)
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
  • You said you don't know how to rollback so why rollback my edit? – styvane Nov 25 '15 at 21:47
  • The rollback button wasn't there until I refreshed the page. I'll stop making edits, you can make yours now. – Brent Washburne Nov 25 '15 at 21:48
  • You should remove the one-liner solution from your answer. – styvane Nov 25 '15 at 21:49
  • This answer is meant to show the solution in a full stepwise program and again in a compact, pythonic one-liner that does the same thing. Don't delete my answer. – Brent Washburne Nov 25 '15 at 21:52
  • My answer also sorts the results on the same line, which makes it different than your answer. – Brent Washburne Nov 25 '15 at 21:53
  • 1
    @user3100115, please stop removing the 1-line solution -- the author of this post has made it clear that they want to include that code in the answer. – josliber Nov 25 '15 at 21:57
  • @josilber please see the edit history. Perhaps you will understand why I edited the first place.. – styvane Nov 25 '15 at 21:59
  • 7
    @user3100115 It looks like you came up with similar solutions within a minute of each other, which usually isn't a case of somebody copying the other. – josliber Nov 25 '15 at 22:01
2

easy way: (Python 3)

d = { 'a' : [1], 'b' : [1,2,3], 'c' : [1,1,2], 'd' : [2,3,4] }  
n = 1
result = []
for key, value in d.items():
    for i in value.count(n):
        res.append(key)

if you want list sorted than:

result.sort()
Enkelli
  • 305
  • 5
  • 18
2

The sort must be made on the dictionary to get the expected result. This should work:

list = []
for i in sorted(d.keys()):
   list+=[i for x in d[i] if x == 1]
print list

will output:

 ['a', 'b', 'c', 'c']
PanGalactic
  • 72
  • 1
  • 8