Remove duplicates and concatenate


yep this works very well :relaxed:
but I have a silly question and I don't know the answer :no_mouth:
I had the same code on this practice but a bit diffrent. it was like this:
def remove_duplicates(lst_number):
for i in lst_number:
if i not in new_list :
new_list= new_list+i
return new_list
but when I ran this code , codecademy showed me an error
what's wrong with using new_list= new_list+i instead of new_list.append(i)?
can you explain please?:confused:

Remove duplicates final

When you append to a list, it already puts the new item at the end of the list so you don't need the following:

new_list= new_list+i

Just use:


and you should be good.


Thank you :relaxed:
yes It worked for me and I'm haven't any problem with that but I'm just want to know why new_list= new_list+i dosen't work?:nerd:


Because we cannot concatenate a string and a list. That is why we use .append(). We can concatenate a list and a list, though.

new_list += [i]

It's not a standard approach, but it is valid, and does work.


Thank you. :smiley:
I got it now. :blush:
Thanks for your helpful answer.:cherry_blossom:


i used the code below ans it work

def remove_duplicates(sequence): #Defines a function
new_list = [] #creates an empty list
for number in sequence: # create a for loop that loops through the numbers in the function input (this case sequence)
for char in new_list:#This loops through the new_list created
if number == char:#if the number sequence is same as number in list, current loop should stop.
else: #an else statement if the above condition is not met, it should append number to the list
return new_list


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