-1

Hi I have a quick question regarding 'if not ..' in py.

This is a function that should take a list of strings and return a string.

I wondered what does the first line(if not strs) do? I guess it checks if the input list strs is empty or not? How does it work? Doesn't 'if not' check if it is opposite of expected value, like 'if not None'? Thank you!

def longestPrefix(strs):

        # Empty list
        if not strs: return ""
         ....
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
JessZhu
  • 15
  • 1
  • 5

1 Answers1

1

Empty strings are False in Boolean context, therefore if not False is True

AlyM.Aly
  • 68
  • 1
  • 6