0

There are total 9 elements which contains the name of drinks. What I need to do is remove any elements that contains "Americano"

However, I am not able to debug my code and it keep shows an error. How can I remove list1's element [0,2,3,6] which contains "Americano"?

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]

while True:
        if " Americano" in list1[i]:
            del list1[0]
    if i < x:
        i = i + 1
        continue

    if i >= x:
        break

print(list1)
정재원
  • 7
  • 1
  • 2
    Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Tim Biegeleisen May 29 '19 at 04:27
  • _and it keep shows an error_ It would help a lot if you **showed** us the error, instead of vaguely mentioning it. – John Gordon May 29 '19 at 04:46

3 Answers3

4

Use a list comprehension to filter out the unwanted elements.

list1 = [x for x in list1 if 'Americano' not in x]

Results:

>>> list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
>>> 
>>> 
>>> list1 = [x for x in list1 if 'Americano' not in x]
>>> list1
[['4', 'Smoothie_queen', '4', '12000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
>>> 
rdas
  • 20,604
  • 6
  • 33
  • 46
1

using filter function, this will create a new list and make result

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
result =list(filter(lambda x: 'Americano' not in x , list1))
print(result)

output

[['4', 'Smoothie_queen', '4', '12000'],
 ['6', 'Americano', '17', '34000'],
 ['7', 'Cafe_mocha', '4', '11200'],
 ['8', 'Cafe_latte', '11', '27500'],
 ['10', 'Amorparty', '2', '4000'],
 ['11', 'Plain_yogurt', '13', '45500']]

using enumerate and list.remove() function, on existing list it modify it accordingly without creating new list

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
for index, value in enumerate(list1):
    if 'Americano' in value:
        list1.remove(value)

print(list1)    

output

[['4', 'Smoothie_queen', '4', '12000'],
 ['6', 'Americano', '17', '34000'],
 ['7', 'Cafe_mocha', '4', '11200'],
 ['8', 'Cafe_latte', '11', '27500'],
 ['10', 'Amorparty', '2', '4000'],
 ['11', 'Plain_yogurt', '13', '45500']]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

if you don't want to be confused Here is Step by Step code

list1 = [['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'],
         ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'],
         ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
lis = []
for row in list1:
    if 'Americano' not in row:
        lis.append(row)
print lis

Output

[['4', 'Smoothie_queen', '4', '12000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]