I have to check that some dictionary has a special key or can I use has_key ?? There is no other method for the key of the dictionary than the items in the list.
I have a list ... [(3,4), (4,5) ..] I need to check that (3,4) in the dictionary.
"true" is the way that other answers are mentioned, such as in operator. This works anything and you get some speed advantage when you can see hashing (such as for dictionaries and sets). There is also an old method which only works for dictionaries which is is the method I usually do not see around these days and it is also slow (although not much).
& gt; & Gt; & Gt; Timeit.timeit ('f = {(1,2): "Foo"}; f.has_key ((1,2))') 0.27945899963378906 & gt; & Gt; & Gt; Timeit.timeit ('f = {(1,2):' Foo '}; (1,2) f') 0.22165989875793457
Comments
Post a Comment