Remove duplicates and concatenate


#1

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):
new_list=[]
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
#2

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:

new_list.append(i)

and you should be good.


#3

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:


#4

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.


#5

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


#6

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.
break
else: #an else statement if the above condition is not met, it should append number to the list
new_list.append(number)
return new_list


#7

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