469,954 Members | 1,706 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,954 developers. It's quick & easy.

appending to dict

Hi
is there anyway appending to dictionary?
list has this feature
a = []
a.append(1)
print a [1]

but dictionary can't
i wanna do like this thinga = {1, 2}
a.append(3, 4) -> This is just my idea :@
print a {1:2, 3:4}

so i try this feature likea = {1, 2}
a = dict(zip(a.keys().append(3), a.values().append(4))) but this make's error
because a.keys().append(3) makes 'None' (i can't understand this :( )

so i tryimport copy
a = dict(zip(copy.copy(a.keys()).append(3),

copy.copy(a.values()).append(4)))
and
a = dict(zip(copy.deepcopy(a.keys()).append(3),
copy.deepcopy(a.values()).append(4)))
but this make's error, too

why a.keys().append(3) makes 'None' ?
and is there anyway appending to dictionary?

any post will be appreciated :)
Jul 18 '05 #1
4 2971


bucket79 wrote:
why a.keys().append(3) makes 'None' ?
Because .append modifies the list in place. It does
not return a new list.
and is there anyway appending to dictionary?


key/value pairs in a dictionary are not ordered. There
is no "last pair". So you can't append to a dictionary.
But you can update it with another dictionary.
Use something like: adict.update (anotherdict)

keys () returns a list of keys in the dictionary.
The order of the elements in this list is arbitrary
(or somehow ordered afterwards). But there is no
real list of keys in the dictionary itself.

Greetings,

Holger

Jul 18 '05 #2
On 14 May 2004 02:40:00 -0700
bu******@daum.net (bucket79) wrote:
Hi
is there anyway appending to dictionary?
list has this feature
a = []
a.append(1)
print a [1]

but dictionary can't
i wanna do like this thinga = {1, 2}
a.append(3, 4) -> This is just my idea :@
print a

{1:2, 3:4}


a = {1:2}
a.update({3:4})

--
Hugh Macdonald
The Moving Picture Company

Jul 18 '05 #3
bucket79 wrote:
is there anyway appending to dictionary?
list has this feature
a = []
a.append(1)
print a [1]

but dictionary can't
i wanna do like this thing
a = {1, 2}
a.append(3, 4) -> This is just my idea :@
print a {1:2, 3:4}


Did you know that the following actually works?
a = {1: 2} # note: your syntax was wrong here
print a {1: 2} a[3] = 4
print a

{1: 2, 3: 4}

Since, as Holger pointed out, dictionaries aren't ordered,
the concept implicit in "append" doesn't apply. The more
appropriate concept "update", however, can be spelled either
dict.update(), or like what I just showed, for dictionaries.

-Peter
Jul 18 '05 #4
On 14 May 2004 02:40:00 -0700,
bu******@daum.net (bucket79) wrote:
is there anyway appending to dictionary?
list has this feature
a = []
a.append(1)
print a [1]

but dictionary can't
i wanna do like this thing a = {1, 2}
a.append(3, 4) -> This is just my idea :@
print a

{1:2, 3:4}


Maybe I'm missing something. Try this:

a = { 1:2 }
a[ 3 ] = 4

and then check out

http://www.python.org/doc/current/tu...00000000000000

and

http://www.python.org/doc/current/lib/typesmapping.html

HTH,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Robin Cull | last post: by
4 posts views Thread by sud jag | last post: by
2 posts views Thread by GrelEns | last post: by
5 posts views Thread by rbt | last post: by
15 posts views Thread by George Sakkis | last post: by
12 posts views Thread by jeremito | last post: by
20 posts views Thread by Pat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.