How to justify a container is a list or dictionary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lv191
    New Member
    • Oct 2007
    • 1

    How to justify a container is a list or dictionary?

    Hi,
    Now i raise a question : How to justify a container is a list or dictionary?

    thank you for your answer !

    Ma Shuai
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by lv191
    Hi,
    Now i raise a question : How to justify a container is a list or dictionary?

    thank you for your answer !

    Ma Shuai
    I think there is a type function that is built in which gives the type pf the object.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      I am sure there is a much more elegant way of doing this (there always is), but here I go anyway:

      [CODE=python]
      >>> a = [1, 2, 3, 4, 5]
      >>> b = dict()
      >>> b['a'] = 1
      >>> b['b'] = 2
      >>> type(a)
      <type 'list'>
      >>> type(b) == type([])
      False
      >>> type(b) == type(dict())
      True
      >>>
      [/CODE]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Here is another way using isinstance():[code=Python]>>> seq = [1,2,3,4,5]
        >>> dd = dict(zip(seq, ['a','b','b','d' ,'e']))
        >>> isinstance(seq, list)
        True
        >>> isinstance(dd, dict)
        True
        >>> isinstance(seq, dict)
        False
        >>> [/code]

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by bvdet
          Here is another way using isinstance():[code=Python]>>> seq = [1,2,3,4,5]
          >>> dd = dict(zip(seq, ['a','b','b','d' ,'e']))
          >>> isinstance(seq, list)
          True
          >>> isinstance(dd, dict)
          True
          >>> isinstance(seq, dict)
          False
          >>> [/code]
          Nice!
          See, that is why I participate in threads like this, I get to learn neat little tricks :D

          Comment

          Working...