You should use the in operator.
The in operator looks up, if an object appears in a sequence.
name for name in A_SEQUENCE if 's' in name.lower
The in operator looks up, if an object appears in a sequence.
In [26]: 's' in 'Andre' Out[26]: False In [27]: 's' in 'Sandra' Out[27]: False In [28]: 's' in 'Sandra'.lower() Out[28]: TrueWith a list:
In [29]: l = [1,2,3] In [30]: 5 in l Out[30]: False In [31]: 1 in l Out[31]: TrueWith filter:
In [32]: list(filter(lambda x: 's' in x.lower(), ['Leon', 'Sucks', 'Mikes', 'Balls'])) Out[32]: ['Sucks', 'Mikes', 'Balls']If you have one finite list and want to have a second list multiplied by 2, you should use a list comprehension:
list1 = [1,2,3] list2 = [i*2 for i in list1]You can use conditions in a list comprehension:
list1 = list(range(100)) list2 = [i*2 for i in list1 if i % 2 != 0]I think the to filter with a list comprehension looks very natural and reads like an english sentence.
name for name in A_SEQUENCE if 's' in name.lower
In [33]: [name for name in ['Leon', 'Sucks', 'Mikes', 'Balls'] if 's' in name.lower()] Out[33]: ['Sucks', 'Mikes', 'Balls']The list-, set-, dict comprehension and generator expressions are very helpful.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!