Check if input is a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Check if input is a List in Python #

Use the isinstance() function to check if an input is a list, e.g. if isinstance(my_list, list):. The isinstance() function will return True if the passed in object stores a list and False otherwise.

main.py
import ast # ✅ Check if variable stores a list my_list = ['a', 'b', 'c'] if isinstance(my_list, list): print('The variable stores a list') print(type(my_list)) # 👉️ <class 'list'> # ------------------------------------------ # ✅ Check if user input is list (e.g. ['a', 'b',]) try: input_list = ast.literal_eval( input('Enter a valid Python list, e.g. tasks=["walk the dog"]: ') ) except ValueError: print('The provided value is not a list') print(input_list) # 👉️ ['a', 'b'] # ------------------------------------------ # ✅ Split value from user input into a list input_list = input('Enter multiple, space-separated values: ').split() print(input_list) # 👉️ ['hello' , 'world']

The first example uses the isinstance() function to check if the provided value is a list.

main.py
my_list = ['a', 'b', 'c'] if isinstance(my_list, list): print('The variable stores a list') print(type(my_list)) # 👉️ <class 'list'>

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

You can use the type class to get the type of an object.

If you need to check if a user input is a list, use the ast.literal_eval() method.

main.py
import ast try: input_list = ast.literal_eval( input('Enter a valid Python list, e.g. ["walk the dog"]: ') ) except ValueError: print('The provided value is not a list') print(input_list) # 👉️ ['a', 'b']

check if input is list

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

This assumes that the expected input is a valid Python list, e.g. [1, 2, 3].

If you need to split multiple values taken from user input into a list, use the str.split() method.

main.py
input_list = input('Enter multiple, space-separated values: ').split() print(input_list) # 👉️ ['hello' , 'world']

The example assumes that you expect to get multiple, space-separated values from the user, e.g. hello world or walk the dog.

The str.split() method splits the string into a list of substrings using a delimiter.

When the str.split() method is called without an argument, it considers consecutive whitespace characters as a single separator.

You can use the same approach if you need to take multiple comma-separated values from user input and split them into a list.

main.py
input_list = input('Enter multiple, comma-separated values: ').split(',') print(input_list) # 👉️ ['hello' , 'world']

The str.split() method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

We used a comma for the separator argument, so the user is expected to provide multiple, comma-separated values, e.g. apple,banana,melon.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.