is this pythonic?

Peter Pearson ppearson at nowhere.invalid
Wed Jan 21 17:32:43 CET 2009


On Wed, 21 Jan 2009 16:16:32 +0100, TP wrote:
>
> Is the following code pythonic:
>
>>>> l=[{"title":"to", "value":2},{"title":"ti","value":"coucou"}]
>>>> dict = [ dict for dict in l if dict['title']=='ti']
>>>> l.remove(*dict)
>>>> l
> [{'title': 'to', 'value': 2}]
>
> Precision: I have stored data in the list of dictionaries l, because in my
> application I am sure that "title" is unique for each record. But perhaps
> it is better to imagine that someday it will not be anymore the case?
[snip]

1. You probably don't want to use the name "dict".

2. I believe this code will fail if the number of dictionaries
   with title="ti" is not exactly 1.  It that your intention?
   (You probably answered this question in the last paragraph
   quoted above, but I can't make it out.)

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list