[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

How to turn a list of tuples into a dictionary?

mrstephengross

2/26/2008 5:01:00 PM

Let's say I've got a list of tuples, like so:

( ('a', '1'), ('b', '2'), ('c', '3')

And I want to turn it into a dictionary in which the first value of
each tuple is a key and the second value is a value, like so:

{ 'a' -> '1', 'b' -> '2', 'c' -> '3' }

Is there a way to do this with a single line of code? Currently, I'm
doing it like this:

tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
d = {}
for option,value in tuples: d[option] = value

Thanks,
--Steve

5 Answers

Aaron Brady

2/26/2008 5:05:00 PM

0

On Feb 26, 11:00 am, mrstephengross <mrstevegr...@gmail.com> wrote:
> Let's say I've got a list of tuples, like so:
>
>   ( ('a', '1'), ('b', '2'), ('c', '3')
>
> And I want to turn it into a dictionary in which the first value of
> each tuple is a key and the second value is a value, like so:
>
>   { 'a' -> '1', 'b' -> '2', 'c' -> '3' }
>
> Is there a way to do this with a single line of code? Currently, I'm
> doing it like this:
>
>   tuples =   ( ('a', '1'), ('b', '2'), ('c', '3')
>   d = {}
>   for option,value in tuples:  d[option] = value
>
> Thanks,
> --Steve

I'd hand-code it manually, by linking a C extension. Or
dict( iterable ).

http://www.python.org/doc/current/lib/built-in-funcs.h...

Paul Rubin

2/26/2008 5:10:00 PM

0

mrstephengross <mrstevegross@gmail.com> writes:
> ( ('a', '1'), ('b', '2'), ('c', '3')

I'm not trying to be snide, but have you tried looking in the manual?

See http://python.o... and look at the section about built-in
types, if you want to know things about tuples or dictionaries.

D'Arcy J.M. Cain

2/26/2008 5:10:00 PM

0

On Tue, 26 Feb 2008 09:00:54 -0800 (PST)
mrstephengross <mrstevegross@gmail.com> wrote:
> Let's say I've got a list of tuples, like so:
>
> ( ('a', '1'), ('b', '2'), ('c', '3')
>
> And I want to turn it into a dictionary in which the first value of
> each tuple is a key and the second value is a value, like so:
>
> { 'a' -> '1', 'b' -> '2', 'c' -> '3' }
>
> Is there a way to do this with a single line of code? Currently, I'm
> doing it like this:
>
> tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
> d = {}
> for option,value in tuples: d[option] = value

How about this?
d = dict(tuples)

--
D'Arcy J.M. Cain <darcy@druid.net> | Democracy is three wolves
http://www.druid.... | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

mrstephengross

2/26/2008 5:15:00 PM

0

> How about this?
> d = dict(tuples)

Aha! I hadn't realized it could be so simple.

--Steve

Aaron Brady

2/26/2008 6:49:00 PM

0

On Feb 26, 11:15 am, mrstephengross <mrstevegr...@gmail.com> wrote:
> > How about this?
> >   d = dict(tuples)
>
> Aha! I hadn't realized it could be so simple.
>
> --Steve

In terms of a metric for code, 'And runs in a single line!' may be a
bit deceptive. [Counterexample snipped.] Absent repeating the line
d= dict( tuples ) verbatim, how does:

identifiers: 3
- func. calls: 1
-- builtins: 1
-- call depth: 1
- assign't: 1
- variable: 1
nesting depth: 1
- parenthesis: 1
- control flow: 0
if statements: 0
- elifs: 0
- elses: 0
call tree: 0
- max. breadth: 1
-- per scope: 1
- max. depth: 1
-- per scope: 1

sound?