mercredi 11 mars 2020

simplify multiple or statements in if else

I have this code to search for text in a list and also parts of the text

  if issi in self.issi_list \
     or issi[:2] in self.issi_list \
     or issi[:3] in self.issi_list \
     or issi[:4] in self.issi_list \
     or issi[:5] in self.issi_list \
     or issi[:6] in self.issi_list:

I hoped to use a generator to replace them like

if issi in self.issi_list or [issi[:x] in self.issi_list for x in range(2, 6)]:

But it does not seem to work. Am I completely on the wrong track?

Aucun commentaire:

Enregistrer un commentaire