Skip to content
Advertisement

How find index where the item length less than x in python

for example i have a list which contain:

['Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
 'Why do we use it?',
 'Where does it come from?',
 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.',
 ....
 'What is Lorem Ipsum?']

For example, I want to get the index of the item that the length is less than 30. How to do that?Is it possible?

Advertisement

Answer

you can use enumerate function and list comprehention for this:

list1 = ['Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
 'Why do we use it?',
 'Where does it come from?',
 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.',
 'What is Lorem Ipsum?']
a = [i for i,ele in enumerate(list1) if len(ele) < 30]
print(a)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement