Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension for dictionaries in dictionaries?

I just learned about list comprehension, which is a great fast way to get data in a single line of code. But something's bugging me.

In my test I have this kind of dictionaries inside the list:

[{'y': 72, 'x': 94, 'fname': 'test1420'}, {'y': 72, 'x': 94, 'fname': 'test277'}] 

The list comprehension s = [ r for r in list if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ] works perfectly on that (it is, in fact, the result of this line)

Anyway, I then realised I'm not really using a list in my other project, I'm using a dictionary. Like so:

{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'}} 

That way I can simply edit my dictionary with var['test1420'] = ...

But list comprehensions don't work on that! And I can't edit lists this way because you can't assign an index like that.

Is there another way?

like image 509
skerit Avatar asked Jul 12 '10 18:07

skerit


People also ask

Does list comprehension work for dictionaries in Python?

The idea of comprehension is not just unique to lists in Python. Dictionaries, one of the commonly used data structures in data science, can also do comprehension.

Can we use list comprehension in dictionary?

Using dict() method we can convert list comprehension to the dictionary. Here we will pass the list_comprehension like a list of tuple values such that the first value act as a key in the dictionary and the second value act as the value in the dictionary.

How do you use list comprehension in Python dictionary?

List comprehensions are constructed from brackets containing an expression, which is followed by a for clause, that is [item-expression for item in iterator] or [x for x in iterator], and can then be followed by further for or if clauses: [item-expression for item in iterator if conditional].

What is the difference between a list comprehension and dict comprehension?

Its syntax is the same as List Comprehension. It returns a generator object. A dict comprehension, in contrast, to list and set comprehensions, needs two expressions separated with a colon. The expression can also be tuple in List comprehension and Set comprehension.


2 Answers

You can do this:

s = dict([ (k,r) for k,r in mydict.iteritems() if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ]) 

This takes a dict as you specified and returns a 'filtered' dict.

like image 75
adamk Avatar answered Sep 22 '22 14:09

adamk


If dct is

{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'},  'test277': {'y': 72, 'x': 94, 'fname': 'test277'},} 

Perhaps you are looking for something like:

[ subdct for key,subdct in dct.iteritems()    if 92<subdct['x']<95 and 70<subdct['y']<75 ] 

A little nicety is that Python allows you to chain inequalities:

92<dct[key]['x']<95 

instead of

if r['x'] > 92 and r['x'] < 95 

Note also that above I've written a list comprehension, so you get back a list (in this case, of dicts).

In Python3 there are such things as dict comprehensions as well:

{ n: n*n for n in range(5) } # dict comprehension {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} 

In Python2 the equivalent would be

dict( (n,n*n) for n in range(5) ) 

I'm not sure if you are looking for a list of dicts or a dict of dicts, but if you understand the examples above, it is easy to modify my answer to get what you want.

like image 42
unutbu Avatar answered Sep 26 '22 14:09

unutbu