By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

appending to dict

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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.