[Python] item for item in list if "s" in name. List comprehension [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    names = ["Milo", "Sarah", "Bruno", "Anastasia", "Rosa"]
    namesWith_O = [name for name in names if ("s" or "S") in name]
    print(namesWith_O)


    The output is :

    ['Anastasia', 'Rosa']


    SImilarly,

    names = ["Milo", "Sarah", "Bruno", "Anastasia", "Rosa"]
    namesWith_O = [name for name in names if ("S" or "s") in name]
    print(namesWith_O)


    The output is:

    ['Sarah']


    Next example:

    name = "Issac Newton"
    if "i" or "c" in name:
    print("True")


    The output is :

    True


    Why are all these behaving differently?

    Continue reading...

Compartilhe esta Página