We have a list of strings here called list_strings `
list_strings =["john", "sarah", "bianca", "savie", "sandy", "john", "harry", "john", "debra", "john"]
If a specified value is "john" and can we get all the strings before john, so the output should look like this!`
result = ["none", "sandy", "harry", "debra]
>Solution :
You can use a simple for loop:
list_strings =["john", "sarah", "bianca", "savie", "sandy", "john", "harry", "john", "debra", "john"]
result = []
for i, j in enumerate(list_strings):
if j == "john":
if i:
result.append(list_strings[i-1])
else:
result.append(None)
[None, 'sandy', 'harry', 'debra']