Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for a char in list of lists

Tags:

python

list

I have a list of lists, and I want to return those sublists that have a specific char.

If the list is:

lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]

I want to retrive ['g', 'j'] "or it's position" if I search using j or g

like image 467
Ali H. El-Kassas Avatar asked Nov 08 '22 10:11

Ali H. El-Kassas


1 Answers

Try this:-

lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
def search(search_char):
    result = [x for x in lst if search_char in x]
    return result
print(search('g'))
like image 191
Narendra Avatar answered Nov 15 '22 04:11

Narendra