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

Time:01-29

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)
]

CodePudding user response:

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)]

CodePudding user response:

new_list is this:

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

'd' isn't in it. Did you want?

new_list = [tup for tup in new_list if 'd' not in tup]
# [('a', 'a', 'a', 'a'), ('b', 'b', 'b', 'b'), ('c', 'c', 'c', 'c')]

In this case, we're checking if 'd' is inside of each tuple in new_list

Edit

Given the new problem formulation, what you want to do is filter the individual tuples. You can do this with a list comprehension:

example = [[item for item in tup if item != '-'] for tup in example]

CodePudding user response:

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.

CodePudding user response:

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)
  •  Tags:  
  • Related