Reputation: 144
I am trying to populate a list with boolean values I have a list for comparision, if the condition satisfies then True if not then False must be added to the list I have tried something like
t_or_f = [True for car in car_types if car in new_car else False]
and
t_or_f = [True for car in car_types if car in new_car True else False]
I know that i can achieve it like this
for car in car_types:
t_or_f.append(car in new_car)
where new_car and car_types are lists , but i need to know how i can minimise my code in this situation using list comprehension
Upvotes: 1
Views: 55
Reputation: 894
how about this:
t_or_f = [car in new_car for car in car_types]
car in new_car will return True or False anyway so no need to make it more complicated
Upvotes: 3