How to find in a list the elements starting by *** in python ?

Active May 26, 2020    /    Viewed 2334    /    Comments 0    /    Edit


Example of how to find in a list the elements starting by *** in python:

1 - - Create a simple list in python

Lets create a liste with words:

l = ['name', 'address_01', 'address_02', 'address_03', 'job', 'income']

2 - - Select words that starts by ***

For example, lets select only the words that start by 'address' using a list comprehension and the python method startswith():

sub_l = [i for i in l if i.startswith('address')]

returns

['address_01', 'address_02', 'address_03']

3 - -Get the indexes of the words that start by ***

To find the indexes, a solution is to use index():

indexes = [l.index(i) for i in sub_l]

[1, 2, 3]

4 -- References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount