Python: Casts the provided value as a list if it's not one
Cast Value to List
Write a Python program to cast the provided value as a list if it's not one.
- Use isinstance() to check if the given value is enumerable.
- Return it by using list() or encapsulated in a list accordingly.
Sample Solution:
Python Code:
Sample Output:
<class 'list'> [1] <class 'tuple'> ['Red', 'Green'] <class 'set'> ['Green', 'Red'] <class 'dict'> [1, 2, 3]
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to cast an object to a list if it is iterable, otherwise return a list with the object as its sole element.
- Write a Python program to safely convert the keys of a dictionary into a list.
- Write a Python program to convert a string to a list of characters only if it is not already a list.
- Write a Python program to transform a tuple into a list when necessary.
Go to:
Previous: Write a Python program to generate a list, containing the Fibonacci sequence, up until the nth term.
Next: Write a Python program to get the cumulative sum of the elements of a given list.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.