Dictionary to tuple

O

Odd-R.

I have a dictionary, and I want to convert it to a tuple,
that is, I want each key - value pair in the dictionary
to be a tuple in a tuple.

If this is the dictionary {1:'one',2:'two',3:'three'},
then I want this to be the resulting tuple:
((1,'one'),(2,'two'),(3,'three')).

I have been trying for quite some time now, but I do not
get the result as I want it. Can this be done, or is it not
possible?


I must also add that I'm new to Python.

Thanks in advance.
 
B

bruno modulix

Odd-R. said:
I have a dictionary, and I want to convert it to a tuple,
that is, I want each key - value pair in the dictionary
to be a tuple in a tuple.

If this is the dictionary {1:'one',2:'two',3:'three'},
then I want this to be the resulting tuple:
((1,'one'),(2,'two'),(3,'three')).

I have been trying for quite some time now, but I do not
get the result as I want it. Can this be done, or is it not
possible?

It's of course possible, and even a no-brainer:

dic = {1:'one',2:'two',3:'three'}
tup = tuple(dic.items())

The bad news is that dict are *not* ordered, so you'll have to sort the
result yourself if needed :(

The good news is that sorting a sequence is a no-brainer too !-)
I must also add that I'm new to Python.
Welcome on board.
 
J

Jeff Epler

It looks like you want tuple(d.iteritems())
((1, 'one'), (2, 'two'), (3, 'three'))

You could also use tuple(d.items()). The result is essentially the
same. Only if the dictionary is extremely large does the difference
matter. (or if you're using an older version of Python without the
iteritems method)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCwWVSJd01MZaTXX0RAiFwAKChDHQYL/lUx6HEHJsbvqq9nSXI/gCfTvT2
CdRYwJqNJH0Emnm86MMavms=
=LyEz
-----END PGP SIGNATURE-----
 
T

Tim Williams (gmail)

I have a dictionary, and I want to convert it to a tuple,
that is, I want each key - value pair in the dictionary
to be a tuple in a tuple.

If this is the dictionary {1:'one',2:'two',3:'three'},
then I want this to be the resulting tuple:
((1,'one'),(2,'two'),(3,'three')).
d = {1:'one',2:'two',3:'three'}
t = tuple([(k,v) for k,v in d.iteritems()])
t ((1, 'one'), (2, 'two'), (3, 'three'))
 
E

Erik Max Francis

bruno said:
Err... don't you spot any useless code here ?-)

(tip: dict.items() already returns a list of (k,v) tuples...)

But it doesn't return a tuple of them. Which is what the tuple call
there does.
 
J

John Roth

bruno modulix said:
It's of course possible, and even a no-brainer:

dic = {1:'one',2:'two',3:'three'}
tup = tuple(dic.items())

I think I'll add a little clarification since the OP is really new
to Python. The (dict.items()) part of the expression returns a
list, and if you want to sort it, then you need to sort the list
and then convert it to a tuple.

dic = {1:'one',2:'two',3:'three'}
dic.sort()
tup = tuple(dic)

This points up the fact that the final conversion to a tuple
may not be necessary. Whether or not is is depends on
the circumstances.
Welcome on board.

John Roth
 
B

bruno modulix

Erik said:
But it doesn't return a tuple of them. Which is what the tuple call
there does.

Of course, but the list-to-tuple conversion is not the point here. The
useless part might be more obvious in this snippet:

my_list = [(1, 'one'), (2, 'two'), (3, 'three')]
my_tup = tuple([(k, v) for k, v in my_list])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top