Open In App

Python list() Function

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python list() Function Syntax

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).

list() Function in Python

We can create a Python list by using list() function. Below are the ways by which we can use list() function in Python:

  • To create a list from a string
  • To create a list from a tuple
  • To create a list from set and dictionary
  • Taking user input as a list

Example 1: Using list() to Create a List from a String

In this example, we are using list() function to create a Python list from a string.

Python3




# 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

In this example, we are using list() function to create a Python list from a Tuple.

Python3




# 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

In this example, we are using list() function to create a Python list from set and dictionary.

Python3




# 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

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


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

In this example, we are using list() function to take user input from the user and create a Python list from that input.

Python3




# 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']



Similar Reads

Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Sort the values of first list using second list in Python
Given two lists, sort the values of one list using the second list. Examples: Input : list1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]Output : ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g'] Input : list1 = ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]O
6 min read
Python List Comprehension | Segregate 0's and 1's in an array list
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Examples: Input : arr = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output : [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] We have existing solution for this problem please refer Segregate 0s and 1s in an array link. We can solve this problem quickly in Python usi
2 min read
Python | Pair and combine nested list to tuple list
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the solution of this problem. Method #1 : Using zip()
10 min read
Python | Filter a list based on the given list of strings
Given a List, the task is to filter elements from list based on another list of strings. These type of problems are quite common while scraping websites. Examples: Input: List_string1 = ['key', 'keys', 'keyword', 'keychain', 'keynote'] List_string2 = ['home/key/1.pdf', 'home/keys/2.pdf', 'home/keyword/3.pdf', 'home/keychain/4.pdf', 'home/Desktop/5.
3 min read
Python program to create a list of tuples from given list having number and its cube in each tuple
Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number. Example: Input: list = [1, 2, 3] Output: [(1, 1), (2, 8), (3, 27)] Input: list = [9, 5, 6] Output: [(9, 729), (5, 125), (6, 216)] Method #1 : Using pow() function.We can use list compreh
5 min read
Python | Sort list of list by specified index
We can sort the list of lists by using the conventional sort function. This sort the list by the specified index of lists. Let's discuss certain ways in which this task can be performed using Python. Method 1: Using the bubble sort algorithm Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each
8 min read
Practice Tags :