Article 6MSFS Why append to list is not the same as +=

Why append to list is not the same as +=

by
ajiten
from LinuxQuestions.org on (#6MSFS)
The first method uses list comprehension, and appends the string in the array 'names' satisfying the given criteria.

First code>
Code:names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
best_list = [name for name in names if len(name)>=6]
print(best_list)Output:
Code:['Kramer', 'Elaine', 'George', 'Newman']----------------------------------------------------------------------
The second method also does the same, as above.

Second code>
Code:names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
if len(name)>5:
better_list.append(name)
print(better_list)Output:
Code:['Kramer', 'Elaine', 'George', 'Newman']----------------------------------------------------------------------------------
If change the second code to the one below, then the individual characters, are stored:

Third code>
Code:names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
if len(name)>5:
better_list+=name
print(better_list)Output:
Code:['K', 'r', 'a', 'm', 'e', 'r', 'E', 'l', 'a', 'i', 'n', 'e', 'G', 'e', 'o', 'r', 'g', 'e', 'N', 'e', 'w', 'm', 'a', 'n']-----------------------------------------------------------------------------------------------------
Question: a) Why the third code, saves the individual characters, is unclear.
b) Also, how can the first code be modified to give the same output as the third code?
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments