Skip to content
Advertisement

Is there a way to target a particular item in a list if it is not the first item [closed]

pist = ['P58', 'P60', 'P0']

def print_it:
   For i in pist:
        if i == 'P0':
            print('yes')

        elif i == 'P58':
            print('yup')

        else:
            print('No')

So this is the issue: I want the function to print ‘yes’ as long as P0 is in the list(irrespective of whether P58 is in it) but it keeps printing ‘yup’ because P58 is the first item on the list. Can there be another way? Note: the list cannot be changed.

Advertisement

Answer

def print():
    if 'P0' in pist:
        return 'yes'
    elif 'P58' in pist:
        return 'yup'
    else:
        return 'No'
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement