Eliminating Repetition Among The Dictionary In Python
Solution 1:
first of all, if you want an empty dictionary, dont do values = []
that will make a list, instead do
values = {}
when adding to a dictionary, do this
mydict[mykey] = myvalue
when checking to see if something is already in the keys do this
if newkey not in mydict:
print('this will not overwrite anything')
I think you are misunderstanding the concept of a dictionary
When you do this key
will be your dictionary key, and val
will be your dictionary value. A dictionary is a combination of pairs of terms in the order {key: value} so if you do myDict[key]
you will get value
If you want to add to a dictionary while making sure that you aren't overwriting anything, this simple example will do that for you.
if newkey not in mydict:
mydict[newkey] = newvalue
Solution 2:
A list is a sequence of elements. Elements are numbered (and ordered) by an implicit index. It is this index what you mainly use to identify the elements within the list. Elements can be repeated. Indexes are not. If you assign a new value to any element (identified by its index), the new value will replace the old one.
Example: In ["day", "midnight", "night", "noon", "day", "night"]
, (implicit) indexes are 0, 1, 2, 3, 4, and 5. list[0]
is "day", list[1]
is "midnight", list[4]
is also "day", and so on. list[3]= "midday"
changes the value of element 3 from "noon" to "midday". list.append("afternoon")
adds an element at the end of the list, which becomes list[6]
.
Lists are useful to represent:
- Collections of (possibly repeated) elements.
- Collections of elements when their order (position in the list) is important.
A dictionary is a collection of elements with no intrinsic order. Elements within the dictionary are identified by explicit keys. As with lists, elements can be repeated, but keys can not, and if you assign a new value to any element (identified by its key), the new value will replace the old one.
Example: In {"dia": "day", "medianoche": "midnight", "noche": "night", "mediodia": "noon", "tag": "day", "nacht": "night"}
keys are "dia", "medianoche", "noche", "mediodia", "tag", and "nacht". dict["dia"]
is "day", dict["medianoche"]
is "midnight", dict["tag"]
is also "day", and so on. dict["mediodia"]= "midday"
would replace the value of the element identified by "mediodia" from "noon" to "midday", and dict["tardes"]= "afternoon"
would add an element for key "tardes" with value "afternoon", as there was no previous element identified by "tardes". This is different to lists, which require append
to add elements.
Dictionaries are useful to represent:
- Associations ("translations", "equivalencies") of data (i.e. of keys into elements, but not the other way round because elements can be duplicate).
- "Lists" with "indexes" that are not integer values (but strings, floating point values, etc)
- "Sparse lists", where keys are integers, but the vast mayority of elements is None. This is usually done to save memory.
Solution 3:
In your code, you are creating an empty list
values = []
Then, you create a dictionary {newvalue : oldvalue}
with only one element oldvalue
whose key is newvalue
. And finally, you add this dictionary to the list through method append
. Yes, in Python it's absolutely valid to have dictionaries as elements of lists and lists as elements of dictionaries (or even their keys). However, this is most probably not what you intended to achieve.
If what you want is a list with elements newvalue
and oldvalue
, you should have written:
values= []
values.append(newvalue)
values.append(oldvalue)
or simply
values= [newvalue, oldvalue]
If what you want is a dictionary with a single element oldvalue
identified by key newvalue
, you should have written:
values= {}
values[newvalue]= oldvalue
or simply
values= {newvalue: oldvalue}
Your code is not working because values
is initially empty, so the for
loop will not iterate at all. What you probably intended is:
values= {}
if newvalue not in values.keys(): # "newvalue not in values" is shorter but can be misleading
values[newvalue]= oldvalue
or
values= {}
if values[newvalue] is None:
values[newvalue]= oldvalue
Additionally, values.update({newvalue: oldvalue})
is a synonym to values[newvalue]= oldvalue
, but you seem to be trying to use a mix of this form and lists by doing values.append({newvalue: oldvalue})
. Again, a dictionary is not a list of single-element dictionaries (which is what you were building and trying to manipulate).
Post a Comment for "Eliminating Repetition Among The Dictionary In Python"