fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
ABOVE CODE WORKS
So I tried running a for loop. What I'm trying to do is, I have 2 lists, and if there is something in my list that contains "a", and if it contains "a", it will be added to the second list
So I did this, just to practice my skills on lists and found that it does not work:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.extend(fruits)
print(newlist)
OUTPUT:
['apple', 'banana', 'cherry', 'kiwi', 'mango', 'apple', 'banana', 'cherry', 'kiwi', 'mango', 'apple', 'banana', 'cherry', 'kiwi', 'mango']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
And I want someone to explain to me step by step what is happening, why I am wrong, and what other methods I can use to get the output I want
WANTED OUTPUT FOR NEWLIST:
['apple', 'banana', 'mango']
** Process exited - Return Code: 0 **
Press Enter to exit terminal