473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 3165


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
4
by: sud jag | last post by:
Hi, Iam a newbie to python. So any help on this is deeply appreciated If I append an instance of a class to a list will the list have a copy of the instance or just a reference to that...
2
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a silly examples to explain my question) class...
5
by: rbt | last post by:
I know how to setup an empty list and loop thru something... appending to the list on each loop... how does this work with dicts? I'm looping thru a list of files and I want to put the file's...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
20
by: Pat | last post by:
I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.