# Dictionaries.update({key:value}) option

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Practice Makes Perfect
remove_duplicates

<In what way does your code behave incorrectly? Include ALL error messages.>
I figured it our eventually, but I’m interested in why my code didn’t work:

<What do you expect to happen instead?>
I realised that that dictionary was not complete since there will be input numbers above 9.
So I used .update({item,1}) if there is a number above nine, make it a key and assign value of 1
since it is mentioned.
Rest of the code works properly.

``````def remove_duplicates(list_):
list_2 =[]
nums_of_duplicates = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
for number_9 in range(len(list_)):
if number_9>9:
nums_of_duplicates({number_9:1})
for key,value in nums_of_duplicates.items():
for item in range(len(list_)):
if list_[item] == key:
value+=1
if value >= 1:
list_2.append(key)

return list_2
``````

indentation is correct

from what i can understand, here you go:

``````def remove_duplicates(list_):
list_2 =[]
nums_of_duplicates = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
for number_9 in range(len(list_)):
if number_9>9:
nums_of_duplicates[list_[number_9]] = 1
print nums_of_duplicates
for key,value in nums_of_duplicates.items():
for item in range(len(list_)):
if list_[item] == key:
value+=1
if value >= 1:
list_2.append(key)

return list_2
print remove_duplicates([4,5,5,4])
``````

you should first increase the value with the second loop, and then place this if condition:

``````if value >= 1:
list_2.append(key)
``````

outside the second loop, otherwise you push to often into list_2.

this method sucks, codecademy also check for 122, you going to write out all this numbers?

i would then create a loop to append the numbers of the list into the dictionary or something

From the beginning I knew to solve code in 4 lines.
The point is that [quote=“programmer_wannabe, post:1, topic:67973”]
if number_9>9:
nums_of_duplicates({number_9:1})
[/quote]
this didn’t work and neither did [quote=“stetim94, post:2, topic:67973”]
if number_9>9:
nums_of_duplicates[list_[number_9]] = 1
[/quote]
The was outside of the second loop, it was an error due to my changes in post.[quote=“stetim94, post:2, topic:67973”]
if value >= 1:
list_2.append(key)
[/quote]
Can you try it?

Pardon:
if number_9>9:
nums_of_duplicates({list_[number_9]:1})

Besides that, that’s this if statement beneath dic.
IF there is a number higher than 9, add it to the dic with value 1.
So I wouldn’t check for all, just for the ones that were inputed.
I could have put empty dic and add all by this method, but I’m trying new things.
Sorry for posting so much lately, but I want to learn as much as I can, and you guys seem
proficient.
Thank you.

and use markup. Also, tell me what code should do what, break it down in piecees

``````def remove_duplicates(list_):
list_2 =[]
nums_of_duplicates = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0} #Dictionary with 9 elements

for number_9 in range(len(list_)):   #Loops through input list
if number_9>9:                                         #If there are any numbers in the list above 9
nums_of_duplicates({list_[number_9]:1})  #Add them to the dictionary above with value 1

for key,value in nums_of_duplicates.items():  #Loops through dictionary's keys and values
for item in range(len(list_)):                       #Loops through input list
if list_[item] == key:          #If there is an item in list that matches with element in dictionary
value+=1                       #Add 1 to value of that element
if value >= 1:                    #If there is any element with value 1 or above
list_2.append(key)          #Append it to the new list

return list_2
``````

start by adding to the dictionary:

``````nums_of_duplicates[number_9] = 1
``````

Tried it, doesn’t work. Didn’t the first time, nor now.
Anything else?

you use range here:

``````for number_9 in range(len(list_)):
``````

just do:

``````for number_9 in list_:
``````

since you are not manipulating the list, range is pointless.

But doesn’t alter the outcome.

Says, dict object is not callable

then you did something wrong, it seems to work now:

``````def remove_duplicates(list_):
list_2 =[]
nums_of_duplicates = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0} #Dictionary with 9 elements

for number_9 in list_   #Loops through input list
if number_9>9:                                         #If there are any numbers in the list above 9
nums_of_duplicates[number_9] = 1  #Add them to the dictionary above with value 1

for key,value in nums_of_duplicates.items():  #Loops through dictionary's keys and values
for item in range(len(list_)):                       #Loops through input list
if list_[item] == key:          #If there is an item in list that matches with element in dictionary
value+=1                       #Add 1 to value of that element
if value >= 1:                    #If there is any element with value 1 or above
list_2.append(key)          #Append it to the new list

return list_2
``````
1 Like

simply don’t use range, it makes the syntax so much easier, including finding your mistake

@stetim94 Thank you very much.
<3

the problem was adding the values to the dictionary, which you did incorrectly

Can you tell me, only, what’s the difference between assigning new key:value point like this:
Dictionary.update({key:value})
and dictionary[key] = value?

update is a built in method, not sure which approach is preferred.

In your case though, the `update` method was missing alltogether, also keep in mind that range gives indexes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.