Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Check if an Object is of Type List in Python?

#1
How to Check if an Object is of Type List in Python?

Problem Formulation: Given a Python object. How to check if the given Python object is of type list?

Example: Given a dictionary for which you don’t know the values associated to the keys. For example, suppose you have entry dict[key] for which you don’t know the type. How to check if it’s a list?

Given: object
Goal: True ---- if object is of type list False ---- otherwise

Method 1: type(object) == list


The most straightforward way to check if an object is of type list is to use Python’s built-in type() function that returns the type of the object passed into it. You can then use the equality operator to compare the resulting type of the object with the list using the expression type(object) == list.

>>> d = {1: [1, 2, 3], 2: 'hello'}
>>> type(d[1]) == list
True

If you check for a non-list, the return value is False:

>>> type(d[2]) == list
False

Note that this is not the most Pythonic solution because Python is a weakly-typed language—you can redefine the name list in your code in which case it wouldn’t work as expected:

>>> d = {1: [1, 2, 3], 2: 'hello'}
>>> list = 42
>>> type(d[1]) == list
False

Wow! This is messy! Suddenly your dictionary element doesn’t seem to be a list anymore! Of course, it still is—the code doesn’t work simply because the name list is now associated to an integer value:

>>> type(list)
<class 'int'>

Therefore, it’s recommended to use the isinstance() built-in method instead to check if an object is of type list.

Method 2: isinstance(object, list)


The most Pythonic way to check if an object is of type list is to use Python’s built-in function isinstance(object, list) that returns either True or False.

>>> d = {1: [1, 2, 3], 2: 'hello'}
>>> isinstance(d[1], list)
True
>>> isinstance(d[2], list)
False

Note that if you’d overwrite the name list with an integer value, Python would at least throw an error (which is a good thing because in contrast to Method 1, you’d now be aware of the mistake)!

>>> list = 42
>>> isinstance(d[1], list)
Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> isinstance(d[1], list)
TypeError: isinstance() arg 2 must be a type or tuple of types

Yet, you should be made aware that using isinstance() is not a end-all be-all fix for this problem of overwriting the name list—it can still be done. However, it would work in fewer cases.

The post How to Check if an Object is of Type List in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 14 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 27 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 20 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 27 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 26 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 29 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 33 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 31 02-03-2021, 01:31 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 28 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 42 02-01-2021, 03:25 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
News - Destiny 2021 Update: The Road to ...
Last Post: xSicKxBot
Today 03:03 AM
» Replies: 0
» Views: 1
News - Hot Wheels Unleashed Speeds Onto ...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 1
Xbox Wire - The Future of Game Accessibi...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 1
News - Get a job: Join Sealost Interacti...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 2
[1.07] BO4 Zombie Trainer (Semi-Patched)
Last Post: trioxyn
Yesterday 08:14 PM
» Replies: 12
» Views: 469
HOW TO: Get all steam games. (Green Luma...
Last Post: trioxyn
Yesterday 07:28 PM
» Replies: 5
» Views: 1688
(Indie Deal) Castlevania, Pqube, Fantasy...
Last Post: xSicKxBot
Yesterday 07:15 PM
» Replies: 0
» Views: 5
Microsoft - Digital good: How to have mo...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - A Super Mario Lenticular Magnet S...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - Halo Infinite's Open World Honors...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 14

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020