append to the end of a dictionary
Yves Glodt
y.glodt at sitasoftware.lu
Tue Jan 24 10:17:59 EST 2006
Paul Rubin wrote:
> Yves Glodt <y.glodt at sitasoftware.lu> 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': 'tcp'}
>> #then:
>> for port,protocol in ports.iteritems():
>> ________print port,protocol
>> ________#do more stuff
>>
>> What would be the appropriate pythonic way of doing this?
>
> ports = [('5631', 'udp'),
> ('5632': 'tcp'),
> ('3389': 'tcp'),
> ('5900': 'tcp')]
>
> for port,protocol in ports:
> print port, protocol # ...
>
> You'd append with
>
> ports.append(('2345', 'tcp'))
>
> note the double set of parentheses since you're appending a tuple.
Tim, Paul, I love you guys !
Thanks a lot
More information about the Python-list
mailing list