By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 440,172 Members | 727 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

Dictionary to tuple

 P: n/a 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. -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne Jul 19 '05 #1
Share this Question
9 Replies

 P: n/a Odd-R. wrote: 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. -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in 'o****@xiludom.gro'.split('@')])" Jul 19 '05 #2

 P: n/a It looks like you want tuple(d.iteritems()) d = {1: 'one', 2: 'two', 3: 'three'} 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----- Jul 19 '05 #3

 P: n/a On 28 Jun 2005 14:45:19 GMT, Odd-R. wrote: 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')) Jul 19 '05 #4

 P: n/a Tim Williams (gmail) wrote: (snip)d = {1:'one',2:'two',3:'three'}t = tuple([(k,v) for k,v in d.iteritems()]) Err... don't you spot any useless code here ?-) (tip: dict.items() already returns a list of (k,v) tuples...) -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in 'o****@xiludom.gro'.split('@')])" Jul 19 '05 #5

 P: n/a bruno modulix wrote: 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. -- Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis But when I reached the finished line / Young black male -- Ice Cube Jul 19 '05 #6

 P: n/a Erik Max Francis wrote: bruno modulix wrote: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. The useless code referred to was the list comprehension. t = tuple([(k,v) for k,v in d.iteritems()]) versus t = tuple(d.items()) or even t = tuple(d.iteritems()) -- Robert Kern rk***@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter Jul 19 '05 #7

 P: n/a Erik Max Francis wrote: But it doesn't return a tuple of them. Which is what the tuple call there does. Yes, but I think he meant: t = tuple(d.items()) -- Jeremy Sanders http://www.jeremysanders.net/ Jul 19 '05 #8

 P: n/a "bruno modulix" wrote in message news:42***********************@news.free.fr... Odd-R. wrote: 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()) 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. I must also add that I'm new to Python. Welcome on board. John Roth -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in 'on***@xiludom.gro'.split('@')])" Jul 19 '05 #9

 P: n/a Erik Max Francis wrote: bruno modulix wrote: 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. 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]) -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in 'o****@xiludom.gro'.split('@')])" Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.