you got a list of strings. print only the string which have an "e" at index(1) or strings which have an odd number of characters in python

You can use list comprehension to achieve that. Here's the code:

main.py
lst = ['hello', 'world', 'thanks', 'for', 'help']
# print strings which have an "e" at index(1)
print([s for s in lst if len(s) > 1 and s[1] == 'e'])
# print strings which have an odd number of characters
print([s for s in lst if len(s) % 2 != 0])
248 chars
6 lines

Output:

main.py
['hello']
['hello', 'world', 'thanks']
39 chars
3 lines

related categories

gistlibby LogSnag