IUnknown
IUnknown

Reputation: 9809

Efficient way to convert a list to dictionary

I need help in the most efficient way to convert the following list into a dictionary:

l = ['A:1','B:2','C:3','D:4']  

At present, I do the following:

mydict = {}
for e in l:
    k,v = e.split(':')
    mydict[k] = v

However, I believe there should be a more efficient way to achieve the same. Any idea ?

Upvotes: 5

Views: 466

Answers (3)

Mp0int
Mp0int

Reputation: 18727

I guess it is better to compare them by execution time...

a = ['A:1','B:2','C:3','D:4']

def case1():
    dc = {}
    for i in a:
        q, w = i.split(':')
        dc[q]=w

def case2():
    dict(x.split(":") for x in a)


def case3():
    {x.split(":")[0] : x.split(":")[1] for x in a}


%timeit -n 100000 case1()
>> 100000 loops, best of 3: 1.95 us per loop


%timeit -n 100000 case2()
>> 100000 loops, best of 3: 3.05 us per loop


%timeit -n 100000 case3()
>> 100000 loops, best of 3: 3.39 us per loop

Tested for 100.000 loops and 3 test for each loop. ;As you can see, fastest execution time belong to case1(): standard for loop.

Result: 1 liner methods do not mean that they are faster, in fact, basic for loop is generally the fastest way to go.

Update: results for a list of 13312 items, basic list have 26 items, rest are the copies of those items wtihin the list. Timing is calculated over 1000 loops and best of 3 for each loop

%timeit -n 1000 case3()
1000 loops, best of 3: 9.49 ms per loop

%timeit -n 1000 case2() 
1000 loops, best of 3: 5.79 ms per loop

%timeit -n 1000 case1()
1000 loops, best of 3: 5.55 ms per loop

Update 2: Final test takes place with a list of 27262976 total items, basic list have 26 items, rest are the copies of those items wtihin the list. Timing is calculated over 10 loops and best of 3 for each loop (since execution of a very long list takes great time).

%timeit -n 10 case1()
10 loops, best of 3: 11.4 s per loop

%timeit -n 10 case2()
10 loops, best of 3: 12.1 s per loop

%timeit -n 10 case3()
10 loops, best of 3: 20.2 s per loop

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

use dict() with a generator expression:

>>> lis=['A:1','B:2','C:3','D:4']
>>> dict(x.split(":") for x in lis)
{'A': '1', 'C': '3', 'B': '2', 'D': '4'}

Using dict-comprehension ( as suggested by @PaoloMoretti):

>>> {k:v for k,v in (e.split(':') for e in lis)}
{'A': '1', 'C': '3', 'B': '2', 'D': '4'}

Timing results for 10**6 items:

>>> from so import *
>>> %timeit case1()
1 loops, best of 3: 2.09 s per loop
>>> %timeit case2()
1 loops, best of 3: 2.03 s per loop
>>> %timeit case3()
1 loops, best of 3: 2.17 s per loop
>>> %timeit case4()
1 loops, best of 3: 2.39 s per loop
>>> %timeit case5()
1 loops, best of 3: 2.82 s per loop

so.py:

a = ["{0}:{0}".format(i**2) for i in xrange(10**6)]

def case1():
    dc = {}
    for i in a:
        q, w = i.split(':')
        dc[q]=w

def case2():
    dict(x.split(":") for x in a)


def case3():
    {k:v for k,v in (e.split(':') for e in a)}

def case4():
    dict([x.split(":") for x in a])

def case5():
    {x.split(":")[0] : x.split(":")[1] for x in a}

Upvotes: 12

richselian
richselian

Reputation: 731

>>> dict(map(lambda s: s.split(":"), ["A:1", "B:2", "C:3", "D:4"]))
{'A': '1', 'C': '3', 'B': '2', 'D': '4'}

Upvotes: 1

Related Questions