Python Forum

Full Version: can't remove element from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a list and i want to remove all non digit element from it,but got some problem.
there is my code , can you help me:

match = re.compile(r'ROL N°.[0-9][0-9].')

case_title = re.sub(match,'',expression) 

 first_case_num =
    for case in first_case_num:
        if case.isdecimal():
AttributeError Traceback (most recent call last)
<ipython-input-91-521c272a0c4b> in <module>
27 print(case)
28 else:
---> 29 first_case_num.remove(case)

AttributeError: 'str' object has no attribute 'remove'
What is num_case?
i updated my code , that you can see . and sorry
As of error I think that this is pretty straightforward: it's shows the row and the problem: first_case_num is string object, not list and therefore Python can't execute this.

But what is your input list and what is expected output?
i really confused,
in my code i want to get 10 first element of a string(in this code it's "expression") and affect them to first_case_num , after that i will remove from first_case_num any element who is not numeric.
I address the task which is described as 'i want to get 10 first element of a string /../ remove /../ any element who is not numeric.'

To get first 10 elements from string one can use slice (just to remind that non-printable characters/elements in string are as their printable counterparts also characters/elements):

>>> s = '123abc456def789'
>>> [char for char in s[:10] if char.isnumeric()]
['1', '2', '3', '4', '5', '6']
>>> ''.join(char for char in s[:10] if char.isnumeric())
thank you for your response, and it help to get see new idea how to solve the problem.
maybe i didn't explain you well and i am sorry.

when i told you 10 characters it was just to give you an example and simplify .
i need to use regex because the expression that i use to extract the digits can contain white spaces or other special characters .