[Tutor] Dictionary Comprehensions
spir
denis.spir at free.fr
Sat Dec 5 11:16:24 CET 2009
Lie Ryan <lie.1296 at gmail.com> dixit:
> note that:
> >>> [(y, x) for y in list("khalid") for x in range(6)]
> [('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('h', 0),
> ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('a', 0), ('a', 1),
> ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('l', 0), ('l', 1), ('l', 2),
> ('l', 3), ('l', 4), ('l', 5), ('i', 0), ('i', 1), ('i', 2), ('i', 3),
> ('i', 4), ('i', 5), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4),
> ('d', 5)]
>
> and when that big list is turned into a dict it gives:
> >>> dict(_)
> {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5}
... because a dict holds a single value per key, so last value overrides previous ones.
Denis
________________________________
la vita e estrany
http://spir.wikidot.com/
More information about the Tutor
mailing list