1

I have a list like this:

list = ['abcd', '1.3', 'a', 'abcdef', '12345']

I need a command that gives me the elements that have length greater than 3 (abdc, abdcef, 12345). How can I do that?

hmd
  • 49
  • 6

2 Answers2

1

[item for item in list if len(item) > 3]

If you want to read up more about this, it's called a "list comprehension".

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
1

I would use a list comprehension like this:

>>> list = ['abcd', '1.3', 'a', 'abcdef', '12345']
>>> [e for e in list if len(str(e)) > 3]
['abcd', 'abcdef', '12345']
>>>
MvdD
  • 22,082
  • 8
  • 65
  • 93