Home > database >  how to fetch whole string if part of the sting is present in list using python
how to fetch whole string if part of the sting is present in list using python

Time:09-12

I have a list with multiple strings in it: for ex: list=['abc/aed','bcd/eac','rtf/reew','opee/rew']

I want to search if 'aed' is present in list if yes fecth whole string "abc/aed"

how to achieve this using python/pyspark

CodePudding user response:

this should work

my_list=['abc/aed','bcd/eac','rtf/reew','opee/rew']
output = [val for val in my_list if "aed" in val]

CodePudding user response:

We can use a list comprehension:

list = ['abc/aed', 'bcd/eac', 'rtf/reew', 'opee/rew']
output = [x for x in list if "aed" in x]
print(output)  # ['abc/aed']

CodePudding user response:

for i in list:
    if "aed" in i:
        print(i)
  • Related