Extract key name from single element dictionary in Python -


If I know that my dictionaries will always have the same elements, then there is a way to remove the key name without going through the list ? I am currently doing this.

  data = {'foo': [1, 2, 3]} key_name = data.keys () [0]  

No more Have a skilled technique?

Change the yield keys.

Use:

  & gt; & Gt; & Gt; Data = {'foo': [1, 2, 3]}> gt; & Gt; & Gt; Next (ITR) 'EFU'  

Benchmark:

 In  [1]: data = {'Foo': [1 , 2, 3]} in [2]:% timeit next (IARAR) 1000000 loops, 3: 1 9 7 best per loop in NS [3]:% timeit data.keys () [0] 10000000 loops, Best 3: 155 ns per loop [4]:% timeit data.iterkeys () Next () 1000000 loops, 3: 226 ns per loop [9]:% timeit k, = data.keys () 100,000,000 loops, best 3: 151 ns per loop [10]:% timeit k, = data # & Lt; --- The fastest 10000000 loops, the best 3: 81.4 loop per loop  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -