The Wayback Machine - https://web.archive.org/web/20241123013313/https://www.geeksforgeeks.org/python-list-function/
Open In App

Python list() Function

Last Updated : 10 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

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

In this example, we are using list() function to create a Python 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

In this example, we are using list() function to create a Python 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
['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.

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

Python list() Function – FAQs

How to get list functions?

To see the available functions and methods for lists in Python, you can use the dir() function with an instance of a list or the list class itself. For example:

print(dir(list))

This will display all the attributes, including methods available for list objects.

Is list() a built-in function?

Yes, list() is a built-in function in Python. It is used to create a new list object from any iterable, like tuples, strings, or other lists. If no iterable is given, it creates an empty list.

How to add lists in Python?

In Python, you can add two lists using the + operator, which concatenates them into a new list. For example:

list_one = [1, 2, 3]
list_two = [4, 5, 6]
combined_list = list_one + list_two
print(combined_list)

This will output: [1, 2, 3, 4, 5, 6].

4. What is a method in Python?

A method in Python is a function that is associated with an object. Methods perform specific actions on an object and can alter the object’s state or return a value. Methods are called on an object using dot notation. For example, the append() method adds an item to the end of a list:

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

This will output: [1, 2, 3, 4].

5. What is get() in Python?

The get() method is used with dictionaries in Python. It retrieves the value for a given key in a dictionary. If the key does not exist, it returns None or a specified default value. This method is beneficial because it does not raise an error for missing keys like direct key access does. For example:

my_dict = {'name': 'Alice', 'age': 25}
print(my_dict.get('name')) # Outputs: Alice
print(my_dict.get('address')) # Outputs: None
print(my_dict.get('address', 'No address provided')) # Outputs: No address provided


Previous Article
Next Article

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 | 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
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 | 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
Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python | Sort list according to other list order
Sorting is an essential utility used in majority of programming, be it for competitive programming or development. Conventional sorting has been dealt earlier many times. This particular article deals with sorting with respect to some other list elements. Let's discuss certain ways to sort list according to other list order. Method #1 : Using List
5 min read
Python | Ways to Convert a 3D list into a 2D list
List is a common type of data structure in Python. While we have used the list and 2d list, the use of 3d list is increasing day by day, mostly in case of web development. Given a 3D list, the task is to convert it into a 2D list. These type of problems are encountered while working on projects or while contributing to open source. Below are some w
3 min read
Python | Merge List with common elements in a List of Lists
Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27, 55], [43, 0, 22], [43, 0, 96], [55, 27, 11]] Out
3 min read
Python | Subtract two list elements if element in first list is greater
Given two list, If element in first list in greater than element in second list, then subtract it, else return the element of first list only.Examples: Input: l1 = [10, 20, 30, 40, 50, 60] l2 = [60, 50, 40, 30, 20, 10] Output: [10, 20, 30, 10, 30, 50] Input: l1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] l2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output: [6, 5,
5 min read
Python - Filter the List of String whose index in second List contains the given Substring
Given two lists, extract all elements from the first list, whose corresponding index in the second list contains the required substring. Examples: Input : test_list1 = ["Gfg", "is", "not", "best", "and", "not", "CS"], test_list2 = ["Its ok", "all ok", "wrong", "looks ok", "ok", "wrong", "thats ok"], sub_str = "ok" Output : ['Gfg', 'is', 'best', 'an
10 min read
Appending Item to Lists of list using List Comprehension | Python
If you are a Python user, you would know that in Python, we can use the append() method to add an item to an existing list. This list may already contain other items or be empty. Further, the item to be added can simply be a number a character, or even an entire tuple or list. However, if you are trying to append an item to lists within a list comp
5 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Apply function to each element of a list - Python
In this article, we will learn how to apply a function to each element of a Python list. Let's see what exactly is Applying a function to each element of a list means: Suppose we have a list of integers and a function that doubles each integer in this list. On applying the function to the list, the function should double all the integers in the lis
2 min read
How to get the list of all initialized objects and function definitions alive in Python?
In this article, we are going to get the list of all initialized objects and function definitions that are alive in Python, so we are getting all those initialized objects details by using gc module we can get the details. GC stands for garbage collector which is issued to manage the objects in the memory, so from that module, we are using the get_
2 min read
Sort a list in Python without sort Function
Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sort() method. Sort a List Without Using Sort Functio
3 min read
How to get list of parameters name from a function in Python?
In this article, we are going to discuss how to get list parameters from a function in Python. The inspect module helps in checking the objects present in the code that we have written. We are going to use two methods i.e. signature() and getargspec() methods from the inspect module to get the list of parameters name of function or method passed as
3 min read
List comprehension and Lambda Function in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples.  What is Calling a Function
5 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
4 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
Filter Python list by Predicate in Python
In this article, we will discuss how to filter a python list by using predicate. Filter function is used to filter the elements in the given list of elements with the help of a predicate. A predicate is a function that always returns True or False by performing some condition operations in a filter method Syntax: filter(predicate, list) where, list
2 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
List View - Function based Views Django
List View refers to a view (logic) to list all or particular instances of a table from the database in a particular order. It is used to display multiple types of data on a single page or view, for example, products on an eCommerce page. Django provides extra-ordinary support for List Views but let's check how it is done manually through a function
3 min read
A Comprehensive Guide to 15 Essential Function for List Manipulation
In the world of Python programming, understanding and using list functions is like having a versatile toolbox at your disposal. Lists, which are flexible structures for storing data, are used in many situations, and knowing how to manipulate them with these functions is crucial. In this article, we will explore essential Python List functions that
4 min read
wxPython - GetField() function function in wx.StatusBar
In this article we are going to learn about GetField() function associated to the wx.GetField() class of wxPython. GetField() function Returns the wx.StatusBarPane representing the n-th field. Only one parameter is required, that is, field number in status bar. Syntax: wx.StatusBar.GetField(self, n) Parameters: Parameter Input Type Description n in
1 min read
List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
Python | Pandas str.join() to join string/list elements with passed delimiter
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.join() method is used to join all elements in list present in a series with passed delimiter. Since strings are also array of
2 min read
Python | Check if all the values in a list that are greater than a given value
Given a list, print all the values in a list that are greater than the given value Examples: Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : YesMethod 1: Traversal of list By traversing in the list, we can compare every element and check if all the elements in the given l
4 min read
Print anagrams together in Python using List and Dictionary
Given an array of words, print all anagrams together. Examples: Input: arr = ['cat', 'dog', 'tac', 'god', 'act'] Output: 'cat tac act dog god' This problem has existing solution please refer Anagrams and Given a sequence of words, print all anagrams together links. We will solve this problem in python using List and Dictionary data structures. Appr
2 min read
Practice Tags :
three90RightbarBannerImg