[Tutor] Tuple and Dicts?
Jason Massey
jason.massey at gmail.com
Thu Nov 9 19:37:29 CET 2006
How about something like:
>>> data = ({'id': 0, 'title': 'Linux'}, {'id': 1, 'title': 'FreeBSD'})
>>> id = 0
>>> result = [x for x in data if x['id'] == id]
[{'id': 0, 'title': 'Linux'}]
>>> result = [x for x in data if x['id'] == id]
>>> result[0]
{'id': 0, 'title': 'Linux'}
>>>
You get the entire dict entry out of the tuple that way.
On 11/9/06, Basil Shubin <bashu at yandex.ru> wrote:
>
> Hi friends!
>
> Imagine the tuple that has dictionaries as it's item:
>
> >>> print data
> ({'id': 0, 'title': 'Linux'}, {'id': 1, 'title': 'FreeBSD'})
>
> How I can get index of tuple item where dict's key 'id' equal to
> appropriate value? And can this be done without 'for' loops, just in one
> string?
>
> --
> Basil Shubin
> Freelance Software Developer
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061109/4e676801/attachment-0001.htm
More information about the Tutor
mailing list