Menu Close

Python list() Function

python list function

In this article, you will learn everything about the Python list() built-in function. The list() function is used to create a list object. Python list() constructor returns a list of passed iterable. Here we will also explore some important functions of Python list.

What is a Python list?

Python list is a collection of items, That are ordered, indexed, changeable or immutable. Python lists allow duplicate items as well. Python provides a lot of list functions that make it very easy to work with python lists.

Python list function

Python list() function is a built-in function that means you need to install it by using the pip command or no need to import it from anywhere. You need to just call the list() function along with the parameter. A parameter must be iterable, If a parameter will not pass it will create an empty list.

Syntax

The syntax of the list() function in Python is.

list(iterable)

list() Parameter

list function in Python takes one parameter.

  • iterable:- Optional. A sequence, collection, or an iterator object. If any iterable will not pass, it will return an empty list.

Python list() Example

In this example section, I am going to tell you all the ways of creating a list, an empty list, a list from a tuple, a list from a string, a list from the set, etc.

Example: Creating an empty list


# Create list object x
x = list()
for i in range(1, 11):
	x.append(i)

print(x)

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example: Converting string to list using list() constructor


# Convert string to list:
string = '1234567'
print("List is:- ", list(string))

Output

List is:-  ['1', '2', '3', '4', '5', '6', '7']

Example: Converting a tuple to a list using list()


# Convert tuple to list:
tuple = (1, 2, 3, 4, 5, 6, 7)
print("Tuple is:- ", tuple)
print("Tuple to List:- ", list(tuple))

Output

Tuple is:-  (1, 2, 3, 4, 5, 6, 7)
Tuple to List:-  [1, 2, 3, 4, 5, 6, 7]

Example: Converting set to list


# Convert set to list
python_set = {'Python', 'Java', 'PHP', 'C++', 'C'}
print("The Python set is:- ", python_set)
print("Set to list:- ", list(python_set))

Output

The Python set is:-  {'PHP', 'C++', 'C', 'Java', 'Python'}
Set to list:-  ['PHP', 'C++', 'C', 'Java', 'Python']

Example: Converting Python dictionary to list


student = {'name': 'Vishvajit', 'age': 25, 'email': '[email protected]', 'occupation': 'developer'}
# converting dictionary to list
print(list(student))

Output

['name', 'age', 'email', 'occupation']
Note:- In the case of a Python dictionary, all the keys will treat items of the list. As you can see in the above example.

Python list useful methods

Python list provides some functions which will apply only to the list. These functions make it easy to work with Python list-objects.

List remove() method

Python list removes method is used to remove the first occurrence of the passed item.It returns ValueError if the passed item is not available in the list.

python_list = ['Python', 'Java', 'PHP', 'C++', 'C', 'Python']
print("List before remove item:- ", python_list)
# remove Python from list
python_list.remove('Python')
print("List after remove item:- ", python_list)
python_list = ['Python', 'Java', 'PHP', 'C++', 'C', 'Python']
print("List before append a new item:- ", python_list)
# append a new item to the list
python_list.append('Dot Net')
print("List after append a new item:- ", python_list)

List append() method

list append method is used to append or add new items to the existing list. It takes a new item as a parameter and appends it to the end of the existing list.

List count() method

Python list count() method is used to return the total number of items present in the list. It returns 0 If the item is not present in the list.

python_list = ['Python', 'Java', 'PHP', 'C++', 'C', 'Python']
total = python_list.count('Pythons')
print(total)
python_list = ['Python', 'Java', 'PHP', 'C++', 'C', 'Python']
lst = ['R', 'Scala', 'Ruby']
total = python_list.extend(lst)
print(python_list)

# Output will be:- ['Python', 'Java', 'PHP', 'C++', 'C', 'Python', 'R', 'Scala', 'Ruby']

List append() method

list append method is used to append or add new items to the existing list. It takes a new item as a parameter and appends it to the end of the existing list.

Conclusion

In this guide, you have seen the Python list() function or constructor to create an empty list or create lists from passed iterable. Python list provides various method that makes it easy to work with list.
If you like this article, please share and keep visiting for further python built-in function tutorials.

Python built-in function


For more information:- Click Here

Python int() Function
Python id() Function

Related Posts