0

I've collected a list of True or false items from a file and converted them into a list of strings:

status = ['True', 'True', 'True', 'True', 'True', 'True', 'True', 'True', 'False']

But I need to input these into a function that only accepts the boolean value of each, but this makes all these strings all be true due to actually having a value inside the string.

In short, here is what I need to convert to:

status = [True, True, True, True, True, True, True, True, False]

So I've tried a few different loop structures as such:

for v in status:
    if v == "True":
        v = True
    if v == "False":
        v = False

However this still gives back the strings in the list, or I've had the list values be deleted. Thanks for any help everybody.

2 Answers2

2

You loop does not actually store the values back to the list. You need to store them:

booleans = []

for v in status:
    if v == "True":
        booleans.append(True)
    else:
        booleans.append(False)

status = booleans

Or in one line:

status = [item == "True" for item in status]
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

One liner:

status = list(map(lambda x: x=='True', status))
RMPR
  • 3,368
  • 4
  • 19
  • 31