Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all index position in list based on partial string inside item in list

mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"] 

I need the index position of all items that contain 'aa'. I'm having trouble combining enumerate() with partial string matching. I'm not even sure if I should be using enumerate.

I just need to return the index positions: 0,2,5

like image 821
L Shaw Avatar asked Feb 13 '13 08:02

L Shaw


People also ask

How do you find the index value of an element in a list?

To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error.


1 Answers

You can use enumerate inside a list-comprehension:

indices = [i for i, s in enumerate(mylist) if 'aa' in s] 
like image 114
StoryTeller - Unslander Monica Avatar answered Sep 25 '22 22:09

StoryTeller - Unslander Monica