Is 'chi' a Boolean generation function in Python?

Randomly selected ‘chi’ as the variant, why does the output list only contain bool values? thanks.

heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]

can_ride_coaster = [chi > 161 for chi in heights]

print(can_ride_coaster)

[False, True, False, False, False, True, True, True, False]

B/c your list comprehension logic loops through the list of heights based on the condition and returns the values that are less than 161cm. You could change “chi” (or, technically, the expression) to anything:

heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = [cat > 161 for cat in heights]

print(can_ride_coaster)

[False, True, False, False, False, True, True, True, False]

It follows this pattern:

[EXPRESSION for ITEM in LIST ]
You can have 0 or more “for” or “if” clauses too.

2 Likes

The thing would be to test the iteration value then assign it.

[x if x > n for x in a]

where a is the iterable, n is the comparison value, and x is a value from the list that meets the condition.

1 Like