Python Lists
Python lists are containers that can hold any type of value. The most important feature of Python Lists is that they are changeable, which means you can change the elements of a list while it is still in use. When you alter a list's member, Python does not create a new list.
A list is a Python data type that can be used to store a succession of values of any type. In Python, square brackets denote lists.
Here are some examples of python lists:
List of numbers
[12, 22, 13]
List of Characters
['c', 'd', 's’]
How to create lists in python?
We can simply create lists in python using square brackets. Below is an example of it.
Python
newList = [1,2,3,4]
print(newList)

You can also try this code with Online Python Compiler
Run Code
Output
[1,2,3,4]
How to access any element of a list in python?
We can simply access the element using its index. For example:
Python
newList = [1,2,3,4]
print(newList[0])

You can also try this code with Online Python Compiler
Run Code
Output
1
So now you have become familiar with lists. Let’s look at some of the Lists Methods
Also see, Python Operator Precedence
Methods of List in Python
Python List append()
Adds a new element to the list's end.
Below is an example of the same.
Python
# animals list
animals = ['cat', 'elephant', 'lion']
# Add 'pig' to the list
animals.append('pig')
print('Updated animals list: ', animals)

You can also try this code with Online Python Compiler
Run Code
Output
[Cat, elephant, lion, pig]
Python List clear()
Below is an example of the same.
All elements in the list are removed.
Python
# list
list = [{1, 2}, ('abdx’), {6,7}]
# Using Method
list.clear()
print('List:', list)

You can also try this code with Online Python Compiler
Run Code
Output
List: [ ]
Python List copy()
This method returns a copy of the list.
Below is an example of the same.
Python
# mixed list
liistXL = ['dog', 0, 6.7]
# copying a list
newList = listXL.copy()
print('Copied List:', newList)

You can also try this code with Online Python Compiler
Run Code
Output
Copied List: [dog, 0, 6.7]
Python List count()
Below is an example of the same.
Python
# list
chars = ['a', 'b', 'c', 'a', 'e']
# count elements
count = chars.count(a)
# print count
print('The count of ‘a’ is:', count)

You can also try this code with Online Python Compiler
Run Code
Output
The count of ‘a’ is 5
Python List extend()
To the end of the current list, append the elements of a list (or any iterable).
Below is an example of the same.
Python
# list
languages = ['English', ‘Dutch’]
# another list
languages1 = ['Spanish']
# appending
languages.extend(languages1)
print('Languages List:', languages)

You can also try this code with Online Python Compiler
Run Code
Output
['English', ‘Dutch’,’Spanish’]
Python List index()
The index of the first element with the specified value is returned.
Below is an example of the same.
Python
animals = ['horse',’racoon’, snake]
# get the index of 'snake'
index = animals.index('snake')
print(index)

You can also try this code with Online Python Compiler
Run Code
Output
2
Python List insert()
Inserts a new element in the specified location.
Below is an example of the same.
Python
# list
lists = [‘1’, ‘2’, ‘3’, ‘5’']
lists.insert(3, 4)
print('List:', lists)

You can also try this code with Online Python Compiler
Run Code
Output
[1, 2, 3, 4, 5]
Python List pop()
Removes the element from the position specified.
Below is an example of the same.
Python
# list
numbers = [2, 3, 5, 7]
numbers.pop(2)
print('Updated List:', prime_numbers)

You can also try this code with Online Python Compiler
Run Code
Output
Updated List: [2, 3, 7]
Python List remove()
The first item with the specified value is removed.
Below is an example of the same.
Python
# list
numbers = [2, 3, 5, 7, 9, 11]
numbers.remove(9)
Updated list
print('Updated List: ', prime_numbers)

You can also try this code with Online Python Compiler
Run Code
Output
Updated List: [2, 3, 5, 7, 11]
Python List reverse()
The list is reordered.
Below is an example of the same.
Python
# list
numbers = [2, 3, 5, 7]
# reversing the order
numbers.reverse()
print('Reversed List:', numbers)

You can also try this code with Online Python Compiler
Run Code
Output
Reversed List: [7, 5, 3, 2]
Python List sort()
sorts the list
Below is an example of the same.
Python
numbers = [11, 3, 7, 5, 2]
prime_numbers.sort()
print(numbers)

You can also try this code with Online Python Compiler
Run Code
Output
[2, 3, 5, 7, 11]
You can compile it with online python compiler.
Must Read Python List Operations
Frequently asked questions
What are lists?
A list is a Python data type that can be used to store a succession of values of any type. In Python, square brackets denote lists. The most important feature of Python Lists is that they are changeable, which means you can change the elements of a list while it is still in use.
What is List comprehension?
In Python, list comprehension is a simple and elegant approach to generating a new list from an existing one. A list comprehension consists of an expression followed by for statement inside square brackets.
What are the common list operations?
The most frequent operations with lists are joining lists, replicating lists, slicing lists, List Membership Tests, Delete List Elements, Accessing elements from the List, and Removing Elements from the List
What is a list function in Python?
A list function in Python is a built-in operation that performs specific actions on list objects, such as adding, removing, or modifying elements.
How do you list all functions in Python?
Use the dir() function on a list object to list all its available methods and attributes. For example: dir(list).
Conclusion
In this article, we have extensively discussed Lists Methods. We hope that this blog has helped you enhance your knowledge of ‘Lists Methods’ and if you would like to learn more, check out our articles on Library where you can find everything about Databases, SQL Probelms, Interview Experiences and other guided paths. Do upvote our blog to help other ninjas grow. Happy Coding!”