[Python] How to write shorter Python code with several if and else statements?

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    How can you optimize the following code and write less?

    class CleanItem():

    def process_item(self, item, spider):

    PSCV = str(item['page_source_canonical']).split("'")[1]

    if PSCV != "":
    if PSCV != item['page_source']:
    item['page_source_canonical_is_itself'] = False
    else:
    item['page_source_canonical_is_itself'] = True
    else:
    item['page_source_canonical_is_itself'] = True


    return item


    First it checks if it is empty. If it is empty, it is true. If it is not empty it should be checked and if it is the same then again it is true. Otherwise it is false.

    Continue reading...

Compartilhe esta Página