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

Published: May 26, 2020

Tags: Python; List;

DMCA.com Protection Status

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