1

I have a list of tuples, all contain 1 phrase and 1 number.

Example: [('light blue', 3), ('light green', 4), ('blue shade', 2), ('deep red', 3), ('dark red')]

I would like to remove tuples from the list which contain certain words.

So let's say I would like to remove tuples containing 'blue' or 'dark in the phrase. How can I do this?

I tried this, but didn't work:

for x in Example:
    if 'blue' in x[0] or 'dark' in x[0]:
        Example.remove(x)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    The last element `dark red` don't have a number? – I'mahdi Nov 03 '21 at 12:53
  • 1
    @user1740577 asks an important question. At the moment, the last item is a str not a tuple. To make it an actual tuple (even if there is no second value) you need `('dark red',)`. If a string is allowed then it will impact the answer. – JonSG Nov 03 '21 at 12:59

3 Answers3

1

You can use something called filter in python. You can create an anonymous function called lambda that will check every first element in your list and according to the boolean value returned by the function it will either keep the value or discard it.

lst = [('light blue', 3), ('light green', 4), ('blue shade', 2), ('deep red', 3), ('dark red')]

list(filter(lambda x: 'blue' not in x[0], lst))
alparslan mimaroğlu
  • 1,450
  • 1
  • 10
  • 20
1

You can create remove_list then check with any and remove element from original list like below:

>>> lst = [('light blue', 3), ('light green', 4), ('blue shade', 2), ('deep red', 3), ('dark red', 1)]
>>> rm_lst = ['blue', 'dark']
>>> [l for l in lst if not any(r_l in l[0] for r_l in rm_lst)]
[('light green', 4), ('deep red', 3)]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
0

Since we've inner data, we've to go to index of outer data, then index of inner data. To do so, iterate the outer data first, for i in example so each value will be a tuple now, after that use i[0] to compare. If condition is satisfied, remove the whole tuple i.e. i. Your code:

Example=[('light blue', 3), ('light green', 4), ('blue shade', 2), ('deep red', 3), ('dark red',)]      #write a comma at the end of (dark red) tuple because it will consider it as string until there's a comma at the end
x=Example.copy()
for i in x:        #i values will be inner tuples
    if "blue" in i [0] or "dark" in i[0]:
        Example.remove(i)       #removing the tuple
print(Example)
  • 1
    You really don't want to `remove()` from a list you are iterating like that. Try removing red by switching `if "blue" in i[0]` to red. – JonSG Nov 03 '21 at 13:07
  • Yeah I tried, it didn't work. Can I know why?? Isn't the condition being satisfied?? "red" is there in the last one, but it isn't getting removed –  Nov 03 '21 at 13:45
  • As you remove items from the list, the loop gets out of sync with where you might think the "current" item is. Typically you would want to use range and iterate backwards from the end so that the indexes don't get messed up as you remove items. – JonSG Nov 03 '21 at 14:00
  • Ok, what if I create a `.copy()` of the list and iterate over the copied list but remove from the orginal one?? Will that work? –  Nov 03 '21 at 14:09
  • 1
    No no, if you use .copy() then the change in the copied list will not make changes in the orginal list. I've edited my answer, please copy that and execute it and let me know –  Nov 03 '21 at 14:41