0

i'm trying to delete all strings after || in the following list:

mylist=[' # - || CAICEDO','LoL','lora',' moco','Sar || var']

For that, i'm using:

def limp_2(n):
         return re.sub(r'^(\w+)-([\||\.])(.+)','',n)

clean_2=list(filter(limp_2,clean_1))
print(clean_2)

The idea is to get:

mylist=[' # - ','LoL','lora',' moco','Sar']

Instead, I just have the same list. That is just an example, I want to apply that to a large list.

I'll appreciate your help.

3
  • 1
    Why is that you don't want to alter first element, but everything after || in some element of the list are removed ? Commented Feb 2, 2020 at 4:59
  • Just use return n.split("||")[0].strip() if you want to do what you asked - see rextester.com/ZYOHO99856 Commented Feb 2, 2020 at 9:14
  • I would like to understand what have you make, what meands [0].strip and how is related with .split. I haven't found the part in [] as a parameter for .split. Thanks again. Commented Feb 2, 2020 at 14:37

1 Answer 1

1

You don't need to use a regex for that. You could simply use something like:

mylist = ['   #  - || CAICEDO', 'LoL', 'lora', ' moco', 'Sar || var']

clean_2 = [mylist[0]] + [s.split('||', 1)[0].rstrip() for s in mylist[1:]]

print(clean_2)

Result:

['   #  - || CAICEDO', 'LoL', 'lora', ' moco', 'Sar']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.