3

I have something like

[('first', 1), ('second', 2), ('third', 3)]

and i want a built in function to make something like

{'first': 1, 'second': 2, 'third': 3}

Is anyone aware of a built-in python function that provides that instead of having a loop to handle it?

Needs to work with python >= 2.6

WojonsTech
  • 1,277
  • 1
  • 13
  • 28

3 Answers3

9

dict can take a list of tuples and convert them to key-value pairs.

>>> lst = [('first', 1), ('second', 2), ('third', 3)]
>>> dict(lst)
{'second': 2, 'third': 3, 'first': 1}
Volatility
  • 31,232
  • 10
  • 80
  • 89
4

Just apply dict() to the list:

In [2]: dict([('first', 1), ('second', 2), ('third', 3)])
Out[2]: {'first': 1, 'second': 2, 'third': 3}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Not as much elegant/simple as dict(my_list) proposed by Volatility's answer...

I would also suggest:

my_list = [('first', 1), ('second', 2), ('third', 3)]
my_dict = {k:v for k,v in my_list}

It's more useful when you have to filter some elements from the original sequence.

my_list = [('first', 1), ('second', 2), ('third', 3)]
my_dict = {k:v for k,v in my_list if k!='third'}

or

my_dict = {k:v for k,v in my_list if test_value(v)} # test_value being a function return bool

as comments says it only works for python >= 2.7

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • any rason why you would suggest this way over the other ways and do you know if this will work in python >= 2.6 – WojonsTech Apr 20 '13 at 08:19
  • 1
    Not over the dict(my_list) that I find most pythonic. But I am/was using list and dict comprehension most of the time, so I do use it for python >= 2.7. I suggest tis one cause most of the time there are condition on the elements that are gonna make the final dictionary, and the dict comprenhension and list comprehension mechanism makes the filtering really easy `my_dict = {k:v for k,v in my_list if k != 'third'}`. I'm adding some other examples to the questions. – Stephane Rolland Apr 20 '13 at 08:24
  • 1
    There is absolutely no reason to use this in this scenario. `dict(...)` is faster, looks better and works on every Python. This **only** works on >= 2.7 – jamylak Apr 20 '13 at 08:25
  • Thanks that helps a lot, I will keep this in mind because it does keep the ability to filter some things that i dont want to copy into the new dict which could be large and a waste of memory and so on. – WojonsTech Apr 20 '13 at 08:27
  • @WojonsTech yep, only if you have things to filter. But like jamylak says in the present case dict(...) is straight way to go. jamylak is right about the 2.6 limitation, it is possible at >= 2.7. This question is a good reference http://stackoverflow.com/questions/1747817/python-create-a-dictionary-with-list-comprehension – Stephane Rolland Apr 20 '13 at 08:33
  • @StephaneRolland I cant use it in this project but i wont forget this trick, I had a feeeling it would not work in 2.6, I had to clear up something simular to it to get the code base for this project to run n 2.6, and the only reason i care about 2.6 is this runs on clients servers were i cant force them to upgrade. – WojonsTech Apr 20 '13 at 08:35
  • Another way to filter: `my_dict = dict(filter(lambda val: val if val[0] != 'second' else None, my_list))` – Denis Nikanorov Apr 20 '13 at 08:50
  • 1
    @Stepane Rolland While filtering is not the case, dictionary appreciation is more efficient when you create dictionary values "on the fly", e.g. {x: x**2 for x in xrange(100)} is faster than dict((x, x**2) for x in xrange(100)). Dictionary appreciation is althouugh more efficient when filtering dictionaries – volcano Apr 20 '13 at 09:14
  • @volcano dictionary comprehension == dictionary appreciation ? I didn't know the term 'appreciation'. – Stephane Rolland Apr 20 '13 at 10:03
  • OK, sorry, my bad - with fixed terminology the comment still stands – volcano Apr 20 '13 at 10:37