0

Im having difficulties completing my code and executing it, can someone help?

Write a function called remove_duplicates that accepts as a parameter a sorted list of strings and returns a new list excluding duplicates from the original list. For example, if the list stores the values ['be', 'be', 'is', 'not', 'or', 'question', 'that', 'the', 'to', 'to'] , the function should return the list with the values ['be', 'is', 'not', 'or', 'question', 'that', 'the', 'to'] . Because the values will be sorted, all of the duplicates will be grouped together. Assume that the list contains only string values, but keep in mind that it might be empty. My code: https://code.sololearn.com/c08BY9CEA8hS/#py a = '' dup_items = set() uniq_items = [] for x in a: if x not in dup_items: uniq_items.append(x) dup_items.add(x) print("original list:", a) print("list after removing duplicates:", dup_items) Output: original list: ['be', 'be', 'is', 'not', 'or', 'question', 'that', 'the', 'to', 'to'] list after removing duplicates: ['be', 'is', 'not', 'or', 'question', 'that', 'the', 'to']

7th Oct 2020, 5:39 PM
...
... - avatar
6 Answers
+ 7
to get your code run, only a small modification is needed: a = ['be', 'be', 'is', 'not', 'or', 'question', 'that', 'the', 'to', 'to'] #a = '' instead of using: a = '' assing your list with words to variable a. --------------- [edited]: this is the code using a for loop without set(): a = ['be', 'be', 'is', 'not', 'or', 'question', 'that', 'the', 'to', 'to'] uniq_items = [] for x in a: if x not in uniq_items: uniq_items.append(x) print("original list:", a) print("list after removing duplicates:", uniq_items)
7th Oct 2020, 5:55 PM
Lothar
Lothar - avatar
+ 6
When using a set() to remove duplicates, you have to keep in mind: - if you have a list of numbers using set() is no problem. duplicates will be removed AND sequence of input is kept. - if your input is a list of words or a list of individual characters, duplicates are removed BUT sequence of input is not kept. in most cases this is no problem, but there are cases where the sequence matters.
7th Oct 2020, 6:02 PM
Lothar
Lothar - avatar
+ 2
once you turn a list to a set, it automatically does that. https://code.sololearn.com/c3667ofx8157/?ref=app
7th Oct 2020, 5:53 PM
Slick
Slick - avatar
+ 2
You didn't make a function. The rest is not so bad https://code.sololearn.com/cojV52UC68Vn/?ref=app
7th Oct 2020, 5:54 PM
Oma Falk
Oma Falk - avatar
0
If sequence matters what would I use instead of set()
7th Oct 2020, 6:24 PM
...
... - avatar
0
Thank you!
7th Oct 2020, 6:32 PM
...
... - avatar