I want ['False', 'False', 'False', 'True', 'True'] not [False, False, False, True, True]

Question:

When getting the output of the for loop in a list, I want the result in multiple items not the whole result in just 1 item.

my_string = "888wT"
the_issue = []


for char in my_string:
    the_issue.append(char.isalpha())

I want the result to be this: Desired result: [‘False’, ‘False’, ‘False’, ‘True’, ‘True’]

but the output is the next: [False, False, False, True, True]

Asked By: J4K0b

||

Answers:

my_string = "888wT"
the_issue = []


for char in my_string:
    if char.isalpha():
        the_issue.append('True')
    else:
        the_issue.append('False')


print(the_issue)

You need to append to the list an String value instead of a Boolean value.

Answered By: Lucas

To get the True/False as string, you can do the following:

my_string = "888wT"
the_issue = [str(char.isalpha()) for char in my_string]
print(the_issue)
Answered By: Subhash Prajapati

you could try list comprehension:

string = "888wT"
the_issue = [str(item.isalpha()) for item in string]
Answered By: INI Corp
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.