Geet Geet - 3 months ago 9
Python Question

How to create a list from another list using specific criteria in Python?

How can I create a list from another list using python?
If I have a list:

input = ['a/b', 'g', 'c/d', 'h', 'e/f']


How can I create the list of only those letters that follow slash "/" i.e.

desired_output = ['b','d','f']


A code would be very helpful.

vks vks
Answer

You probably have this input.You can get by simple list comprehension.

input = ["a/b", "g", "c/d", "h", "e/f"]
print [i.split("/")[1] for i in input if i.find("/")==1 ]

or

print [i.split("/")[1] for i in input if "/" in i ]

Output: ['b', 'd', 'f']