How to find whether the list or dictionary is empty?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    How to find whether the list or dictionary is empty?

    Hi,

    How to find whether the list or dictionary is empty?.

    Thanks
    PSB
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by psbasha
    Hi,

    How to find whether the list or dictionary is empty?.

    Thanks
    PSB
    len()
    Since this post is too short, I have to add some text to it: "Python Essential Reference", 3rd Edition

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by psbasha
      Hi,

      How to find whether the list or dictionary is empty?.

      Thanks
      PSB
      The interactive interpreter is there so that you may try things quickly:
      Code:
      >>> d = {}
      >>> if d:
      ...     print "d has entries"
      ...     
      >>> if not d:
      ...     print "empty"
      ...     
      empty
      >>> l = []
      >>> if not l:
      ...     print "empty list"
      ...     
      empty list
      >>>

      Comment

      Working...