2

I am trying to extract a part of a string and append it to a list (message). But at the end, what I see is just one item appended (the last one) to the list. Below is my code. What am i doing wrong?

for item in all_text:
    message = []
    if len(item) < 2:
        continue
    else:
        m_temp = item.split(']')[1].split(':')
        if len(m_temp) <= 1:
            continue
        else:
            message.append(m_temp[1])
    print(len(message))
    print(message)

2 Answers 2

1

move the message = [] above the for loop and align the two print with the For

Sign up to request clarification or add additional context in comments.

Comments

0

You're redefining message to be an empty list with each iteration of your for loop. Therefore, all of your previous appends will be lost except for the one on the last iteration.

Try moving message = [] to right before your for loop.

1 Comment

move the message = [] above the for loop and align the tow print with the For

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.