1

I have a list of data:

data = ['value', '"""', 'Comment 1st row', 'Comment 2nd row', 'Comment 3rd row', '"""', 'another value']

I would like to remove the whole comment from the list, including docstrings. Also not just once, but everytime a comment appears.

Could someone help?

3
  • what's the data characteristics: array of strings? How are the string comments different to the value ones? what do they contain in particular? Commented Oct 21, 2022 at 7:57
  • It means you want to remove all items that appear between '"""? '"""', 'Comment 1st row', 'Comment 2nd row', 'Comment 3rd row', '"""' Commented Oct 21, 2022 at 7:57
  • Everything in a list is a string. Yes I want to remove everything between """ including. Commented Oct 21, 2022 at 7:59

4 Answers 4

3

You can use a simple loop with a flag that alternates every time a """ is found:

data = ['value', '"""', 'Comment 1st row', 'Comment 2nd row',
        'Comment 3rd row', '"""', 'another value']

flag = True
out = []
for v in data:
    if v == '"""':
        flag = not flag  # invert the flag's boolean value
        continue         # we have a comment, skip to the next step
    if flag:             # if flag is True, add the item
        out.append(v)

print(out)

output:

['value', 'another value']

Example run on data = data * 5 to mimic multiple comments:

['value', 'another value', 'value', 'another value',
 'value', 'another value', 'value', 'another value',
 'value', 'another value']
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this seems to be working on it's own (I will try to implement it in the more complex function). I'm just learning, could you explain to me what does flag = not flag do? Is it the same as flag = False? Thanks
No, this is inverting the boolean value of flag (True->False / False -> True)
Awesome I went through it line-by-line to understand the logic and I kind of get it now. Cool stuff, thanks
1

Try this:

data = ['value', '"""', 'Comment 1st row', 'Comment 2nd row', 'Comment 3rd row', '"""', 'another value']

newData = []

comment = False

for word in data:
    if word == '"""':
        comment = not comment
    elif not comment:
        newData.append(word)

print(newData)

Comments

1

You can do with re,

import re
sp_chr = '$'
list(filter(None, re.sub(r'""".*"""', '', sp_chr.join(data)).split(sp_chr)))

Output:

['value', 'another value']

$ is a unique character used to separate the elements in the list. You have the freedom to choose any values there.

1 Comment

A built-in version list(filter(None, ''.join('$'.join(data).split('"""')[::2]).split('$')))
0

A counter-based approach. If the amount of """ is even then append (comments are balanced), instead increment the counter if odd (since comment hasn't finished).

res = []
c = 0
for char in data:
    if char != '"""':
        if not c % 2:
            res.append(char)
    else:
        c += 1

print(res) 

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.