append to the end of a dictionary

Y

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
 
J

Juho Schultz

Yves said:
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'
 
R

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'}
mydict['b'] = '2'
print mydict {'a': '1', 'b': '2'}
{'a': '1', 'b': '2'} == {'b': '2', 'a': '1'}
True
 
Y

Yves Glodt

Rene said:
Yves Glodt:

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. 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?


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'}
mydict['b'] = '2'
print mydict {'a': '1', 'b': '2'}
{'a': '1', 'b': '2'} == {'b': '2', 'a': '1'}
True
 
T

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,protocol in ports.iteritems():
________print port,protocol
________#do more stuff

What would be the appropriate pythonic way of doing this?

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 you to use the same port with both UDP and TCP. If
you want to ensure that only one pair (port+protocol) can be in
the list, you can use a set() object:

set(ports)

(or in earlier versions:

from sets import Set
s = Set(ports)

which I happened to have here)

This will ensure that you don't end up with more than one item
with the same port+protocol pair.

-tkc
 
P

Paul Rubin

Yves Glodt said:
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.
 
Y

Yves Glodt

Paul said:
Yves Glodt said:
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
 
R

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 you could create a dictionary with a tuple (protocol, port) as key, and
whatever data you need to associate with it as value.

mydict = { ('udp',5631): 'value1',
('tcp',3389): 'value2' }
 
S

Steven D'Aprano

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', '5632': 'tcp', '3389': 'tcp', '5900': 'tcp'}

Which will work perfectly fine as a dict, because you have unique keys but
multiple values. Two keys with the same value is allowed.

Question: is there a reason the port numbers are stored as strings instead
of numbers?

#then:
for port,protocol in ports.iteritems():
________print port,protocol
________#do more stuff


What would be the appropriate pythonic way of doing this?


Is it even possible to have multiple protocols listening on the
same port? Doesn't matter I suppose. Associating multiple values to the
same key is easy: just store the values in a list:


foods = { "red": ["apple", "tomato", "cherry"],
"green": ["apple", "lettuce", "lime"],
"blue": ["blueberry"],
"yellow": ["butter", "banana", "apple", "lemon"] }

# add a new colour
foods["orange"] = ["orange"]
# add a new food to a colour
foods["red"].append("strawberry")

for colour, foodlist in foods.iteritems():
for food in foodlist:
print colour, food


If you need to use the keys in a particular order, extract the keys and
sort them first:

colours = foods.keys()
colours.sort()
for colour in colours:
for food in foods[colour]:
print colour, food
 
M

Magnus Lycka

Tim said:
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 you to use the same port with both UDP and TCP. If you want
to ensure that only one pair (port+protocol) can be in the list, you can
use a set() object:

But again, order will be lost (if that is of any consequence).

Another option would be to have a dict of sets keyed on port:

In Python 2.3:
.... if port not in ports:
.... ports[port]=sets.Set()
.... ports[port].add(prot)
....
>>> add_port(25, 'tcp')
>>> add_port(25, 'udp')
>>> add_port(80, 'tcp')
>>> ports {80: Set(['tcp']), 25: Set(['udp', 'tcp'])}
>>> add_port(80, 'tcp')
>>> ports
{80: Set(['tcp']), 25: Set(['udp', 'tcp'])}

Dicts have the obvious advantage that you can look up the protcols
used on a port without iterating over some sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,096
Latest member
Kenkian2628

Latest Threads

Top