简体   繁体   中英

How to Update values (List forms) from while loop in dictionary python

I don't really understand the concept of python dictionary, can anyone help me? I want the program to have similar functionality as append in list python

d = {'key': ['value']}
print(d)
# {'key': ['value']}
d['key'] = ['mynewvalue']
print(d)
# {'key': ['mynewvalue']}

what I want the output of the program, either:

print(d)
#{'key': ['value'],'key': ['mynewvalue']}

or:

print(d)
#{'key': ['value','mynewvalue']}

Sure: first thing first, you can't have two identical keys in a dictionary. So:

{'key': 'myfirstvalue', 'key': 'mysecondvalue'}

wouldn't work. If a key has multiple values, then the key's value should be a list of values, like in your last option. Like in a real dictionary, you won't find, word: definition, word: another definition but word: a list of definitions.

In this regard, you could kind of think of a dictionary as a collection of variables - you can't assign two values to a variable except by assigning a list of values to variable.

x = 4
x = 5

is working code, but the first line is rendered meaningless. x is only equal to 5, not both 4 and 5. You could, however, say:

x = [4, 5]

I often use dictionaries for trees of data. For example, I'm working on a project involving counties for every state in the US. I have a dictionary with a key for each state, and the value of each key is another dictionary, with a key for each county, and the value for each of those dictionaries is another dictionary with the various data points for that county.

That said, you can interact with your dictionary just like you would with variables.

mylist = [1, 2, 3, 4]
mylist.append(5)
print(mylist)

will print: [1,2,3,4,5]

But also:

mydict = {'mylist': [1,2,3,4]}
mydict['mylist'].append(5)

does the same thing. mydict['mylist'] is the same as mylist in the first example. Both are equal to the list [1,2,3,4]

For the first implementation you want, I think you are violating the entire idea of dictionary, we can not have multiple keys with the same name.

For the second implementation you could write a function like this:

def updateDict(mydict,value):
    mydict['key'].append(value)

You cannot have same keys multiple times in a dict in python. The first output scenario you gave is invalid. The value of a dict can contain any data and in your case, it can be accessed and modified just as a list. You can modify the code as given below to get the output as desired in scenario number 2.

d = {'key': ['value']}
print(d)
# {'key': ['value']}
d['key'].append('mynewvalue')
print(d)
#{'key': ['value','mynewvalue']}

you can try it:

d = {'key': ['value']}
d['key'].append("mynewvalue")
print(d)

Output will be: {'key': ['value', 'mynewvalue']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM