Remove_duplicate challenge myself using dictionary format *can anyone optimize this dirty code for me?


#1



remove_duplicate (https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#)


Hi, guys. I am stuyding python and be good at it. I wanted to try to use "dictionary format" this time.
I know it isn't the best way to solve this problem.

but I wanted to store each element in list in dictionary key and start value for each element with 0.
Then iterate through the list and count it and save the count in the value in dictionary.

Then I will check if value of key is greater than 0 (which means that the element has been found at least one)
It doesn't matter how many count I get. as long as it is greater than 0 then I print the qualified_key list.

If you can think of any better way for this using dictionary , I would appreciate it so much :slight_smile


optimize


####my code start here #############
'''
def remove_duplicates(li): #list
    list_dic = {} #create empty dictionary
    qaulified_key = []
    count = 0
    
    #store number in list in dictioray key with value(count) 0
    for i in li:
        print i
        list_dic[i] = 0
    print list_dic
    
    # iterate list, count the value and update it in corresponding dictionary value
    for j in list(list_dic):
        for numb in list_dic.keys():
            print "j:" + str(j), "numb:" + str(numb)
        count = numb

    if count > 0:
        print "qualified keys in dictionary: " + str(list(list_dic))
        qaulified_key = list_dic

    return qaulified_key
        
remove_duplicates([1,2,2, 3, 3])


#########################

'''
# shortest way
'''
def remove_duplicates(lst):
    return list(set(lst))
'''

# common way
'''
def remove_duplicates(dup_list):
    no_dup = []
    print "dup_list:" + str(dup_list)
    for element in dup_list:
        if element not in no_dup:
            no_dup.append(element)
    print "no_dup:" + str(no_dup)
    return no_dup

print remove_duplicates([1,1,2,2])
'''


#2

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