list() function in Python is used to create lists from iterable objects. An iterable is an object that can be looped over, such as strings, tuples, sets, dictionaries and other collections. The function provides a convenient way to convert these objects into lists, making it easier to manipulate and process their elements. Example:
Python
s = "Python"
# Using list()
res = list(s)
print(res)
Output['P', 'y', 't', 'h', 'o', 'n']
Explanation: String “Python” has six characters. list(s) converts it into [‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’] by iterating through each character.
list() syntax
list(iterable)
Parameters:
- iterable (optional): An iterable object (such as a string, tuple, set, dictionary, or another iterator) that is converted into a list.
- If no argument is passed, it returns an empty list: [].
Returns: Returns a new list containing elements from the given iterable.
Examples of list()
Examples 1: Creating a list from a tuple
Python
tup = (10, 20, 30, 40)
# converting tuple to list
res = list(tup)
print(res)
Explanation: Tuple tup has four elements. list(tup) converts it into [10, 20, 30, 40] by iterating through each element.
Example 2: Creating a List from a Set
Python
a = {1, 2, 3, 4, 5}
# converting set to list
res = list(a)
print(res)
Explanation: Set a has five elements. list(a) converts it into a list, but the order may vary since sets are unordered.
Example 3: Creating a list from a dictionary
Python
d = {'A': 10, 'B': 20, 'C': 30}
# Converting dictionary to list
res = list(d)
print(res)
Explanation: dictionary d has three key-value pairs. list(d) converts it into [‘A’, ‘B’, ‘C’], as list() extracts only the dictionary keys.
Example 4: Taking user inputs as a list
Python
# taking user input
a = list(input("Enter list elements: "))
print(a)
Output
Enter list elements: Hello
['H', 'e', 'l', 'l', 'o']
Explanation: This code takes user input as a string and converts it into a list of individual characters using list().
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].
Similar Reads
Python list() Function
list() function in Python is used to create lists from iterable objects. An iterable is an object that can be looped over, such as strings, tuples, sets, dictionaries and other collections. The function provides a convenient way to convert these objects into lists, making it easier to manipulate and
3 min read
Python Functions
Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it ov
11 min read
Python input() Function
Python input() function is used to take user input. By default, it returns the user input in form of a string. input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input message Ex: input("What is your name? ") Returns: Return a string value as input by the user.
4 min read
Python print() function
The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python min() Function
Python min() function returns the smallest of the values or the smallest item in an iterable passed as its parameter. Example: Find Python min integer from the list [GFGTABS] Python numbers = [23,25,65,21,98] print(min(numbers)) [/GFGTABS]Output 21Python min() Function Syntaxmin(a, b, c, ..., key=fu
4 min read
Python len() Function
len() function returns number of items in an object that can be characters, words, or elements in a sequence. Letâs start with a simple example to understand the len() functions with strings: [GFGTABS] Python s = "GeeksforGeeks" # Use len() to find the length of the string length = len(s)
2 min read
Python oct() Function
Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
2 min read
Python Main Function
Main function is like the entry point of a program. However, Python interpreter runs the code right from the first line. The execution of the code starts from the starting line and goes line by line. It does not matter where the main function is present or it is present or not. Since there is no mai
5 min read
Python 3 - input() function
In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string. Python input() Function SyntaxSyntax: input(prompt) Parameter: Prompt: (optio
3 min read
Python - globals() function
In Python, the globals() function is a built-in function that returns a dictionary representing the current global symbol table. In this article, we will see the Python globals() function. globals() Function in PythonPython globals() function is a built-in function in Python that returns the diction
2 min read