Why do these 2 codes should work similiarly but don't?

1st code:

number = ["Python" , "Java" , "R" , "C" , "PHP", "Python" , "Java" , "R" , "C" , "PHP"]
num = []
for x in number:
    if not x in num:
        num += x
print(num)

result : [‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘J’, ‘a’, ‘v’, ‘a’, ‘R’, ‘C’, ‘P’, ‘H’, ‘P’]

2nd code :

number = ["Python" , "Java" , "R" , "C" , "PHP", "Python" , "Java" , "R" , "C" , "PHP"]
num = []
for x in number:
    if not x in num:
        num.append(x)
print(num)

result :[‘Python’, ‘Java’, ‘R’, ‘C’, ‘PHP’]

please help i’m new to python

I tried to understand but I didn’t so I came here to understand.
I expected both their results to be the same…

Using += where the LHS is a list is equivalent to the list’s .extend method, not its .append method.

The .extend method iterates over its argument, and iterating over a string yields the characters of the string (as strings).

1 Like

Thanks, you don’t know how much this means to me bro, I asked the same question on Stack overflow and got flagged for a duplicate for a question about something else and the post got downvoted. Thank you for your help and time given

It’s important to understand that Stack Overflow is not for getting personalized help with an issue, discussing an example, being tutored, etc. Stack Overflow is for asking a clear, specific question, which involves:

  • Asking a question in the post itself (not just the title)
  • Explaining your reasoning
  • Giving the question a title that describes what you’re asking about (as a rule: try not to use the word “this” or “these”, because the point is to know what kind of problem it is before looking at any code. Try to think of what you would want the page to look like, if you found it with a search engine. Try to think about what it would need to look like, so that you could find it with a search engine.)
  • Making sure the code appears properly formatted (pay attention when someone else edits your post and make sure you don’t undo a formatting fix if you have to edit something else in. Check the post preview before submitting a question or an edit.)
  • Not talking about yourself, saying that you’re a beginner, asking people to be nice, etc. etc. etc., because a question on Stack Overflow is about the question, not the person asking it.

There is more detailed advice in the Help section on Stack Overflow:

I was able to find your question pretty easily (I just searched for Python questions that were closed as duplicates, most recently). It is indeed a duplicate - because this is a very common problem, which is the same reason you could get an answer quickly here. The duplicate Q&A you got was wrong, because it’s hard to search for the right one - because there are so many questions on the site already (and almost all of them are not properly written) and because different problems often use all the same words to explain them. I managed to find a better target and fixed that. I also edited your question to show proper style.

Please do not feel bad if your Stack Overflow question is closed as a duplicate. There are something like 24 million open questions on Stack Overflow already (more than three times as many questions, as there are articles on Wikipedia); the site is 15 years old, and you are trying to ask a basic question about a programming language that has existed for… longer than that. It is basically impossible that your question will be new. Of course it’s preferred that you search first (and questions like yours will get downvoted, because they don’t help build the site and don’t do a good job of asking the question); but if you don’t find it, and someone else finds it for you, then you get your answer anyway. Downvotes on questions are, again, about the question, not you; we’re trying to shuffle things out of the way that don’t help with building the site, according to the goals of the site.

Please feel free to ask whatever beginner questions you like here on discuss.python.org. That’s why there’s a site like this - a discussion forum, to discuss a problem where you need more personalized assistance.

1 Like

Stackexchange is much more strict about duplicate questions. They are nicer here.

Also I stick to mylist.append() and mylist.extend().

Sticking to mylist.extend(...) is a good idea because mylist += ... rebinds to mylist, which could trip you up if you’re doing it in a function and mylist exists outside that function (you would have to declare that mylist is global or nonlocal).