Re: append to the end of a dictionary

2006-01-25 Thread Sion Arrowsmith
I wrote: >orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())] > >(k, mydict[k]) sets off warning bells for me that mydict.items() >should be in use [ ... ] But apparently [ x for x in mylist ] doesn't yet trigger such a warning. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~

Re: append to the end of a dictionary

2006-01-25 Thread Fuzzyman
Tim Chase wrote: > > I seem to be unable to find a way to appends more keys/values to the end > > of a dictionary... how can I do that? > > > > E.g: > > > > mydict = {'a':'1'} > > > > I need to append 'b':'2' to it to have: > > > > mydict = {'a':'1','b':'2'} > > my understanding is that the order

Re: append to the end of a dictionary

2006-01-25 Thread Sion Arrowsmith
Magnus Lycka <[EMAIL PROTECTED]> wrote: >orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())] Or: orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())] (k, mydict[k]) sets off warning bells for me that mydict.items() should be in use, which are nearly as loud as the on

Re: append to the end of a dictionary

2006-01-25 Thread Justin Azoff
Magnus Lycka wrote: > orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())] orderedListOfTuples = sorted(mydict.items()) > It's great that many people try to help out on comp.lang.python, > the community won't survive otherwise, but I think it's important > to test answers before

Re: append to the end of a dictionary

2006-01-25 Thread Magnus Lycka
Tim Chase wrote: > I would lean towards using tuples, as in > > ports = [('5631','udp'), ('5632', 'tcp'), ('3389','tcp'), ('5900','tcp')] > > which you can then drop into your code: > > for (port, protocol) in ports: > print port, protocol > #do more stuff > > This allows yo

Re: append to the end of a dictionary

2006-01-25 Thread Magnus Lycka
Paul Rubin wrote: > ports = [('5631', 'udp'), > ('5632': 'tcp'), > ('3389': 'tcp'), > ('5900': 'tcp')] Change the colons to commas, and this will work... -- http://mail.python.org/mailman/listinfo/python-list

Re: append to the end of a dictionary

2006-01-25 Thread Magnus Lycka
Tim Chase wrote: > orderedDict = [(k,mydict[k]) for k in mydict.keys().sorted()] Wrong. As you stated, sorted() is a function, not a method. orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())] It's great that many people try to help out on comp.lang.python, the community won't s

Re: append to the end of a dictionary

2006-01-24 Thread Steven D'Aprano
On Tue, 24 Jan 2006 08:45:27 -0600, Tim Chase wrote: > To get them in a key-order, my understanding is that you have to > use something like > > keys = mydict.keys() > sort(keys) > orderedDict = [(k, mydict[k]) for k in keys] Did you test this before posting? >>> keys = ["a",

Re: append to the end of a dictionary

2006-01-24 Thread Steven D'Aprano
On Tue, 24 Jan 2006 16:01:48 +0100, Yves Glodt wrote: > that means I can neither have a dictionary with 2 identical keys but > different values...? Yes, that's correct. > I would need e.g. this: > (a list of ports and protocols, to be treated later in a loop) > > > > ports = {'5631': 'udp',

Re: append to the end of a dictionary

2006-01-24 Thread Tim Chase
>> orderedDict = [(k,mydict[k]) for k in mydict.keys().sort()] >> >>Unfortunately, the version I've got here doesn't seem to support >>a sort() method for the list returned by keys(). :( > > I bet it does, but it doesn't do what you think it does. See > http://docs.python.org/lib/typesseq-mutabl

Re: append to the end of a dictionary

2006-01-24 Thread Sion Arrowsmith
Tim Chase <[EMAIL PROTECTED]> wrote: >unless you have a way of doing an in-line sort, in which you >would be able to do something like > > orderedDict = [(k,mydict[k]) for k in mydict.keys().sort()] > >Unfortunately, the version I've got here doesn't seem to support >a sort() method for the li

Re: append to the end of a dictionary

2006-01-24 Thread Rene Pijlman
Yves Glodt: >I would need e.g. this: >(a list of ports and protocols, to be treated later in a loop) [...] >What would be the appropriate pythonic way of doing this? In the case of tcp and udp ports, it's the combination of protocol and port number that's unique, not the port number by itself. So

Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Paul Rubin wrote: > Yves Glodt <[EMAIL PROTECTED]> writes: >> that means I can neither have a dictionary with 2 identical keys but >> different values...? > > No. > >> I would need e.g. this: >> (a list of ports and protocols, to be treated later in a loop) >> >> ports = {'5631': 'udp', '5632': '

Re: append to the end of a dictionary

2006-01-24 Thread Paul Rubin
Yves Glodt <[EMAIL PROTECTED]> writes: > that means I can neither have a dictionary with 2 identical keys but > different values...? No. > I would need e.g. this: > (a list of ports and protocols, to be treated later in a loop) > > ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 't

Re: append to the end of a dictionary

2006-01-24 Thread Tim Chase
> that means I can neither have a dictionary with 2 identical keys but > different values...? correct :) > I would need e.g. this: > (a list of ports and protocols, to be treated later in a loop) > > ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 'tcp'} > #then: > for port,protoc

Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Yves Glodt wrote: > Hi there, > > I seem to be unable to find a way to appends more keys/values to the end > of a dictionary... how can I do that? > > E.g: > > mydict = {'a':'1'} > > I need to append 'b':'2' to it to have: > > mydict = {'a':'1','b':'2'} > > > > How to do? Sorry for the no

Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Rene Pijlman wrote: > Yves Glodt: >> I seem to be unable to find a way to appends more keys/values to the end >> of a dictionary > > A dictionary has no order, and therefore no end. that means I can neither have a dictionary with 2 identical keys but different values...? I would need e.g. thi

Re: append to the end of a dictionary

2006-01-24 Thread jay graves
Yves Glodt wrote: > I seem to be unable to find a way to appends more keys/values to the end > of a dictionary... how can I do that? A dictionary doesn't have an 'end' because it is an unordered collection. > E.g: > mydict = {'a':'1'} > I need to append 'b':'2' to it to have: mydict['b'] = '2' p

Re: append to the end of a dictionary

2006-01-24 Thread Fredrik Lundh
Yves Glodt wrote: > I seem to be unable to find a way to appends more keys/values to the end > of a dictionary... how can I do that? > > E.g: > > mydict = {'a':'1'} > > I need to append 'b':'2' to it to have: > > mydict = {'a':'1','b':'2'} to add stuff to a dictionary, use item assignment: m

Re: append to the end of a dictionary

2006-01-24 Thread Rene Pijlman
Yves Glodt: >I seem to be unable to find a way to appends more keys/values to the end >of a dictionary A dictionary has no order, and therefore no end. >mydict = {'a':'1'} > >I need to append 'b':'2' to it to have: > >mydict = {'a':'1','b':'2'} > >How to do? Like this: >>> mydict = {'a':'1'} >

Re: append to the end of a dictionary

2006-01-24 Thread Tim Chase
> I seem to be unable to find a way to appends more keys/values to the end > of a dictionary... how can I do that? > > E.g: > > mydict = {'a':'1'} > > I need to append 'b':'2' to it to have: > > mydict = {'a':'1','b':'2'} my understanding is that the order of a dictionary should never be rel

Re: append to the end of a dictionary

2006-01-24 Thread Juho Schultz
Yves Glodt wrote: > Hi there, > > I seem to be unable to find a way to appends more keys/values to the end > of a dictionary... how can I do that? > > E.g: > > mydict = {'a':'1'} > > I need to append 'b':'2' to it to have: > > mydict = {'a':'1','b':'2'} > mydict['b'] = '2' -- http://mail.py

append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Hi there, I seem to be unable to find a way to appends more keys/values to the end of a dictionary... how can I do that? E.g: mydict = {'a':'1'} I need to append 'b':'2' to it to have: mydict = {'a':'1','b':'2'} How to do? Best regards, Yves -- http://mail.python.org/mailman/listinfo/pyt