append to the end of a dictionary
Paul Rubin
http
Tue Jan 24 10:08:25 EST 2006
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.
More information about the Python-list
mailing list