'I'm trying to remove values from a list but it says the value doesn't exist

example = [
    ('0-2', '0-0', '2-3', '0-1', '1-1', '1-0', '3-3', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'), 
    (4, 6, 7, 9, 10, 13, 17, 20, 24, 27, 29, 30, 31, 33, 35, 36, 37, 38, 45, 48, 51, 58, 61, 71, 79), 
    ('4-2', '2-0', '0-1', '2-1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'), 
    (6, 12, 13, 25, 30, 35, 37, 46, 47, 56, 58, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-')
]

example.remove('-')

print(example)
    new_list.remove('-')
ValueError: list.remove(x): x not in list

How should I proceed so that this error no longer occurs?

Expected result:

[
    ('0-2', '0-0', '2-3', '0-1', '1-1', '1-0', '3-3'), 
    (4, 6, 7, 9, 10, 13, 17, 20, 24, 27, 29, 30, 31, 33, 35, 36, 37, 38, 45, 48, 51, 58, 61, 71, 79), 
    ('4-2', '2-0', '0-1', '2-1'), 
    (6, 12, 13, 25, 30, 35, 37, 46, 47, 56, 58)
]


Solution 1:[1]

The items you want to remove are not in the list example, but in the tuples in the list. However, tuples are immutable, meaning that you cannot do remove on them. Instead, you can use (generator) comprehension to filter out unwanted items:

example = [
    ('0-2', '0-0', '2-3', '0-1', '1-1', '1-0', '3-3', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'),
    (4, 6, 7, 9, 10, 13, 17, 20, 24, 27, 29, 30, 31, 33, 35, 36, 37, 38, 45, 48, 51, 58, 61, 71, 79),
    ('4-2', '2-0', '0-1', '2-1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'),
    (6, 12, 13, 25, 30, 35, 37, 46, 47, 56, 58, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-')
]

output = [tuple(x for x in tup if x != '-') for tup in example]
print(output)
# [('0-2', '0-0', '2-3', '0-1', '1-1', '1-0', '3-3'), (4, 6, 7, 9, 10, 13, 17, 20, 24, 27, 29, 30, 31, 33, 35, 36, 37, 38, 45, 48, 51, 58, 61, 71, 79), ('4-2', '2-0', '0-1', '2-1'), (6, 12, 13, 25, 30, 35, 37, 46, 47, 56, 58)]

Solution 2:[2]

One best way would be to see what does new_list holds in the memory, like this... enter image description here

Thus, as you can see, new_list holds, a list of tuples, that's why you're getting an error when you're trying to remove a single element 'd' from it.

Solution 3:[3]

The reason why there is no 'd' in the list is that the list is actually of tuppels of chars and not just of chars. You can see what a mean by this when you print the list before you remove 'd':

[('a', 'a', 'a', 'a'), ('b', 'b', 'b', 'b'), ('c', 'c', 'c', 'c'), ('d', 'd', 'd', 'd')]

I asume you want to flatten the list and then remove the char. In this case you can do the following to actually flatten the list:

example = [['a', 'b', 'c', 'd'],['a', 'b', 'c', 'd'],['a', 'b', 'c', 'd'],['a', 'b', 'c', 'd']]

new_list = []

for nested in example:
    for item in nested:
        new_list.append(item)

new_list.remove('d')

print(new_list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MK14
Solution 3 StackOverflower