Skip to content
Related Articles

Related Articles

Python list() Function

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 11 Nov, 2021

Python list() function takes any iterable as a parameter and returns a list. In Python iterable is the object you can iterate over. Some examples of iterables are tuples, strings, and lists.

Syntax:

list(iterable)

Parameter:

  • iterable:  an object that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object.

Note: If we don’t pass any parameter then the list() function will return a list with zero elements (empty list).

Lets see some examples for better understanding.

Example 1: Using list() to create a list from a string

Python




# initializing a string
string = "ABCDEF"
 
# using list() function to create a list
list1 = list(string)
 
# printing list1
print(list1)


Output:

['A', 'B', 'C', 'D', 'E', 'F']

Example 2: Using list() to create a list from a tuple

Python




# initializing a tuple
tuple1 = ('A', 'B', 'C', 'D', 'E')
 
# using list() function to create a list
list1 = list(tuple1)
 
# printing list1
print(list1)


Output:

['A', 'B', 'C', 'D', 'E']

Example 3: Using list() to create a list from set and dictionary

Python




# initializing a set
set1 = {'A', 'B', 'C', 'D', 'E'}
 
 
# initializing a dictionary
dictionary = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
 
 
# using list() to create a list
list1 = list(set1)
list2 = list(dictionary)
 
# printing
print(list1)
print(list2)


Output:

['C', 'E', 'D', 'B', 'A']
['A', 'B', 'C', 'D', 'E']

We can also use list() function while taking input from user to directly take input in form of a list.

Example 4: Taking user input as a list

Python




# Taking input from user as list
list1 = list(input("Please Enter List Elements: "))
 
# printing
print(list1)


Output:

Please Enter List Elements: 12345
['1', '2', '3', '4', '5']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!