Re: Dictionary to tuple
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 '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
"bruno modulix" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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 [EMAIL PROTECTED] "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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 && [EMAIL PROTECTED] && 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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 '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
On 28 Jun 2005 14:45:19 GMT, Odd-R. <[EMAIL PROTECTED]> 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')) >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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 pgpegdipnTdVc.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary to tuple
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 '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Dictionary to tuple
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 -- http://mail.python.org/mailman/listinfo/python-list