Delete a list with fewer than a certain number of characters in the list in Python

Asked 2 years ago, Updated 2 years ago, 14 views

parseList =[ 'J', ']-', 'Track-927', '4']
firstlist = parseList.split()
for s in firstlist:
    if "-" in s:
        print(s)

result from - and Track-927 are shown.

How do I remove items with fewer than a few characters from the list as a conditional statement?

python

2022-09-21 10:01

2 Answers

First of all, I can't split the list, but I think the result of split the string is parseList.

Also, based on the question alone, if the number of characters is less than the reference value, I'm not sure if it's subtracted from the output list or removed from the list at all, but I wrote it thinking it was the former case.

parseList =[ 'J', ']-', 'Track-927', '4']
n = 3
for s in parseList:
    if "-" in s and len(s) > n:
        print(s)

Enter the desired number of characters in n.


2022-09-21 10:01

''.join([x for x in parseList if '-' in x and len(x) > 3])

Then do it this way


2022-09-21 10:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.