G
G
Govesinc2021-08-16 19:53:27
Python
Govesinc, 2021-08-16 19:53:27

How to create a copy of a list that only contains elements that meet a condition?

Original :
["11", "2", "3", "4", "55"]
condition: len(element) > 1
Copy would be:
["11", "55"]

Is it possible to do this without loops? Are there built-in solutions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-08-16
@Govesinc

>>> lst = ["11", "2", "3", "4", "55"]
>>> new_lst = filter(lambda x: len(x) > 1, lst)
>>> new_lst
['11', '55']

S
soremix, 2021-08-16
@SoreMix

original = ["11", "2", "3", "4", "55"]
new = [el for el in original if len(el) > 1]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question