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]
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.
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)
you could try list comprehension:
string = "888wT"
the_issue = [str(item.isalpha()) for item in string]
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]
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.
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)
you could try list comprehension:
string = "888wT"
the_issue = [str(item.isalpha()) for item in string]