A
A
Andrew2021-11-30 01:26:39
Python
Andrew, 2021-11-30 01:26:39

How to select the necessary elements from the list?

There is a list of models consisting of str and int:

models = ['chevrolet', 24, 'buick', 7, 'gmc', 7, 'cadillac', 6, 'daewoo', 0, 'geo', 0, 'holden', 0, 'hummer', 0, 'isuzu', 0, 'oldsmobil', 0, 'oldsmobile', 0, 'opel', 0, 'pontiac', 0, 'ravon', 0, 'saab', 0, 'saturn', 0, 'truck', 0, 'vauxhal', 0, 'vauxhall', 0]

How optimally (without a multi-stage for) to select only those values ​​of str, after which int is not equal to 0 (while excluding int itself), i.e. in the list above, you need to get it['chevrolet', 'buick', 'gmc', 'cadillac']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-11-30
@rsytrade

[ models[i] for i in range(0, len(models), 2) if models[i + 1] ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question