Python list() Function: Use, Syntax, and Examples

Python list() function: In this tutorial, we will learn about the list() function in Python with its use, syntax, parameters, returns type, and examples. By IncludeHelp Last updated : June 23, 2023

Python list() function

The list() function is a library function in Python, it is used to create a list, and it accepts multiple elements enclosed within brackets (because the list() takes only one argument. Thus, the set of elements within brackets is considered as a single argument).

Consider the below example with sample input/output values:

Input:
students = list(("Amit shukla", "prem", "Radib", "Abhi"))

Output:
students:  ['Amit shukla', 'prem', 'Radib', 'Abhi']

Syntax

The following is the syntax of list() function:

list((elements))

Parameter(s):

The following are the parameter(s):

  • elements – list of the elements.

Return Value

The return type of list() function is <class 'list'>, it returns a list of given elements.

Python list() Example 1: Create a list with given set of elements

# python code to demonstrate example of
# list() method 

# creating list
students = list(("Amit shukla", "prem", "Radib", "Abhi"))

# printing type of list() function
print("type of list() function: ", type(students))

# printing list...
print("students: ", students)

Output

type of list() function:  <class 'list'>
students:  ['Amit shukla', 'prem', 'Radib', 'Abhi']

Python list() Example 2: Convert string to a list

# Example to convert string to a list
# using list() method

# string
vowels = "AEIOU"

# convert string to list
vowels_list = list(vowels)

# print
print("vowels_list:", vowels_list)

Output

vowels_list: ['A', 'E', 'I', 'O', 'U']

Python list() Example 3: Create lists from set and dictionary

# set
x = {10, 20, 30, 40}
print(list(x))

# dictionary
y = {"a": "Apple", "b": "Banana", "c": "Cat"}
print(list(y))

Output

[40, 10, 20, 30]
['a', 'b', 'c']


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.