Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'LIKE' function for Lists

Tags:

python

Is there any equivalent 'LIKE' function(like in MySQL) for lists. for example;

This is my list:

abc = ['one', 'two', 'three', 'twenty one']

if I give the word "on", it should print the matching words from the list (in this case: 'one', 'twenty one') and if I give "fo", it should print False

like image 741
Maggie Avatar asked Jan 24 '26 07:01

Maggie


1 Answers

You can use list comprehension:

[m for m in abc if 'on' in m]

This roughly translates to "for each element in abc, append element to a list if the element contains the substring 'on'"

like image 144
Manny D Avatar answered Jan 26 '26 20:01

Manny D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!