Dictionaries.update({key:value}) option


#1



Practice Makes Perfect
remove_duplicates


I figured it our eventually, but I'm interested in why my code didn't work:


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


#2

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


#3

From the beginning I knew to solve code in 4 lines.
The point is that

this didn't work and neither did

The was outside of the second loop, it was an error due to my changes in post.

Can you try it? :slight_smile:


#4

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


#5

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.


#6

please post an updated version of your code:

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


#7

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

#8

start by adding to the dictionary:

nums_of_duplicates[number_9] = 1

#9

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


#10

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.


#11

But doesn't alter the outcome.
Uploading...


#12

Says, dict object is not callable


#13

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

#14

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


#15

@stetim94 Thank you very much.
<3


#16

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


#17

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?


#18

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


#19

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