Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operations on Lists
2.1.
Code to Create a List
2.2.
Python
3.
Access Python List Elements
3.1.
Example: 
3.2.
Python
3.3.
Output:
4.
Negative Indexing in Python
4.1.
Example:
4.2.
Python
4.3.
Output:
5.
List Operations in Python
5.1.
1. Append()
5.2.
Python
5.3.
2. extend()
5.4.
Python
5.5.
3. Insert()
5.6.
Python
5.7.
4. remove()
5.8.
Python
5.9.
5. Pop()
5.10.
Python
5.11.
6. Slice()
5.12.
Python
5.13.
7. reverse()
5.13.1.
Example: 
5.14.
Python
5.14.1.
Output:
5.15.
8. len(), min() & max()
5.16.
Python
5.17.
9. count()
5.18.
Python
5.19.
10. Concatenate
5.19.1.
Example:
5.20.
Python
5.20.1.
Output:
5.21.
Python
5.21.1.
Output:
5.22.
11. Multiply
5.22.1.
Example:
5.23.
Python
5.23.1.
Output:
5.24.
12. index()
5.24.1.
Example:
5.25.
Python
5.25.1.
Output:
5.26.
13. sort()
5.27.
Python
5.28.
14. clear()
5.28.1.
Example:
5.29.
Python
5.29.1.
Output:
6.
List Methods
7.
Python List Comprehension
7.1.
Example
8.
Python
8.1.
Output:
9.
Frequently Asked Questions
9.1.
How many operations does a list have in Python?
9.2.
How do you speed up Python List Operations?
9.3.
How do you apply an operation to a list in Python?
9.4.
What is the fastest type of list in Python?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python List Operations

Author Tashmit
2 upvotes
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Do you know what a list is? Have you ever performed any operations on a list in Python? If not, then don't worry; we got your back.

Python List Operations

In Python, a list is a type of data structure that enables users to store information in sequence to constitute a record. Its indexing starts from 0; a list can keep any data (integer, character, boolean, string). Before we jump to Python list operations, it is recommended you go through the python data types. Let us discuss the characteristics of a list.

Operations on Lists

A list in Python is built using square brackets. We write the values inside the square brackets separated by commas. 

Code to Create a List

  • Python

Python

#Creating an empty list
new_list = []

#List with string
new_list=['Coding', 'Ninjas']
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Access Python List Elements

If suppose there is a Python list that has more than one element and the user wants to access a particular element from that list, then that can be done by just declaring that index in square brackets “[ ]”. As we know, indexing starts at 0, so to access the first element of a list, we need to input 0 into the square brackets, and so on for subsequent elements.

Example: 

  • Python

Python

coding_list = ["coding", "ninjas", "data", "science"]   #This is a list declared under name: coding_list
print(coding_list[1]) #Accessing the 2nd element (index 1) of the list

Output:

output

Also see, Python Filter Function

Negative Indexing in Python

Python Lists and Strings allow element accessibility from the end as well, without writing any long codes. If there is a long list with an enormous number of elements and the user wants to access an element that is positioned in the last indexes, rather than traversing the whole list from the start (index 0), the user can simply access the last elements by declaring the index with the “-” sign, which indicates negation.

Example:

  • Python

Python

coding_list = ["coding", "ninjas", "data", "science"]   #This is a list declared under name: coding_list
print(coding_list[-2]) #Accessing the 2nd last element (index 3) of the list

Output:

output

List Operations in Python

Below are some of the commonly used list operations in python:

1. Append()

As we know, a list is mutable. We can add an element at the back of the list using an inbuilt function of Python list operation append(). Let us see implementations of it.

  • Python

Python

my_list=[1,2,3]
my_list.append(9)
my_list.append(8)
print("The list after append() operation is: ",my_list)

Here, we created a list called my_list and added elements to the list. The output of the above code is:

Output:

output

2. extend()

Extend () method can be used to add more than one element at the end of a list. Let us understand the function by its implementation.

  • Python

Python

my_list.extend([20,21])
print("The list after axtend() operator is: ",my_list)

Output:

output

By entering the values in a square bracket inside the function extend, we can see that all the elements are added to the list.

3. Insert()

Now we can insert an element in between the list using the inbuilt function of Python list operation insert()

  • Python

Python

my_list.insert(5,30)
print("The list after insert() operator is: \n",my_list)

In this function, the first input is the position at which the element is to be added, and the second input is the element's value. Here after using the insert() we have added 30 at 5th position in my_list. Hence the updated list will be:

output

4. remove()

The way we added an element between the list, similarly, we can remove from in between a list using an inbuilt Python list operation function remove()

  • Python

Python

my_list.remove(10)
print("The list after remove() operator is: \n",my_list)

As we have removed 10 from my_list, the output of the above code will be:

output

We can see that element 10 is removed from the list. 

5. Pop()

There is an inbuilt function to remove the last element from the list known as pop()

  • Python

Python

my_list.pop()
print("The list after pop() operator is:\n",my_list)

After using pop() operator, the last element would be removed from our my_list list. Here the last element, 5, is removed from the list. Hence our updated list is

output

6. Slice()

This method is used to print a section of the list. Which means we can display elements of a specific index. 

  • Python

Python

print("The elements of list in the range of 3 to 12 are:\n",my_list[3:12])

Using the slicing operator, we have sliced elements from range of 3 to 12 in the my_list. Hence the above code would print elements whose index lies between the range 3 to 12. 

output

Note that the slice method only prints the elements; it does not change the list. 

7. reverse()

To reverse the indexing of elements in a list, the simple built-in function reverse() can be used. This function allows the user to reverse the order of elements very quickly and without large code lines. To reverse a list, it needs to be parsed with the function itself.

Example: 

  • Python

Python

coding_list = ["coding", "ninjas”, "data", "science"]  
coding_list.reverse() #Reverse Function implemented
print(coding_list)

Output:

['science', 'data', 'ninjas', 'coding']

8. len(), min() & max()

Other Python list operations are len(), min(), and max(). As the name suggests, they are used to know the length of a list, the minimum element in the list, and the maximum element in the list, respectively. 

  • Python

Python

print("Length of the list is: ",len(my_list))
print("Maximum element in the list is: ",max(my_list))
print("Minimum element in the list is: ",min(my_list))

The length, maximum and minimum element in my_list are:

output

9. count()

As we can store duplicates in a list, a Python list operation is used to count the number of copies, known as count().

  • Python

Python

my_list.extend([10,20,22])
print("The list is: \n",my_list)
print("Number of times 21 is in the list are: ",my_list.count(20))

After using the extend() and count() operator on my_list, the the number of repetitions of 21 are:

output

Here 2 shows the number of occurrences of element 23. 

10. Concatenate

Concatenate is a very simple process, with the literal meaning of combining or joining two objects. Similarly, concatenation in Python can be used to combine two lists or strings. Concatenation in Python can be simply performed by using “+” between variable names that hold a string or list. 

Example:

  • Python

Python

list_one = [100, 150, 200]
list_two = [900, 950, 800]
new_list = list_one + list_two #new_list variable holds the concatenated list
print(new_list)

Output:

output

Similarly, following is an example to perform concatenation on a string:

  • Python

Python

string_one = ["Coding"]
string_two = ["Ninjas"]
new_string = string_one + string_two
print(new_string)

Output:

output

11. Multiply

Did you know that you can increase the number of elements in a list by simply multiplying it by a number? For example, by multiplying a list with the number ‘n’, you get that list element being repeated ‘n’ times. List multiplication can be performed by just using the “*” operator. Following is a code example:

Example:

  • Python

Python

list_one = [100, 150, 200]
list_two = list_one * 3 #Multiplied the list with 3
print(list_two)

Output:

output

12. index()

index() is a built-in Python function that lets users access the elements of a list, tuple, or string. This is a really useful function, especially when the data inside the list or tuple is very large. Note that if there are duplicate entries in the list, tuple, or string, then the first occurrence of that particular element will be represented in the output.

Example:

  • Python

Python

list_one = [100, 150, 200]
print(list_one.index(100)) #Using index function for a list

Output:

output

13. sort()

The most used function while using a list is sort(). With the help of the function, we can sort the list(my_list) in ascending order.  

  • Python

Python

my_list=[8,10,4,1,3,19,11,9,20]
my_list.sort()
print("The list after sort() operator is: \n",my_list)

We can see the sorted my_list as the output. 

output

So far, we've seen various Python list operations using inbuilt functions. Now let us look at some arithmetic and logical operations in lists. 

14. clear()

If you want to clear all the elements present in a list, set, or dictionary, then clear() is one of the most efficient and easy built-in functions to use. It is important to note that this can be implemented on a list, set, or dictionary only. Using this function, you can clear all the contents, and the result is None.

Example:

  • Python

Python

list_one = [100, 150, 200]
list_one.clear() #This clears all the elements present in the list
print(list_one)

Output:

output

List Methods

Function

Description

append()

Adds an element to the end of the list

extend()

Adds more than one element to the end of the list

insert()

Adds an element in between the list

remove()

Removes an elements from the list

pop()

Removes the last elements from the list

slice()

Prints a section of the list

reverse()

Reverses the order of the elements in a list

len()

Gives the length of a list

min()

Gives the minimum element (by value) of a list

max()

Gives the maximum element (by value) of a list

count()

Counts the number of copies in a list

Concatenate

Combines two list

Multiply

Multiplies the occurrence of elements in a list

index()

Gives index number of an element in the list

sort()

Sorts the list in ascending order

clear()

Clears every element in a list

 

Python List Comprehension

List Comprehension is one of Python’s most efficient ways to work on a list and implement conditions on a list, all in just one line! The list comprehension expression mainly consists of a command, an iterable variable, and the condition to be implied. It is a highly useful technique to decrease code lines and make the codebase more accessible.

Example

  • Python

Python

list_one = [100, 150, 200, 250, 300]
list_cubic = [number ** 3 for number in list_one] #This line generates a list with cubes of all elements in the list_one
print(list_cubic)

In the code above, number ** 3 is the expression or command, and number is the iterable variable in that list.

Output:

output

Frequently Asked Questions

How many operations does a list have in Python?

There are not one but many operations that can be performed on a list, depending on the expression. The operations include accessing the list using index() or slice(), or modifying the list by using pop(), remove(), or append().

How do you speed up Python List Operations?

The speed of list operations can be increased by using efficient techniques like List Comprehension or built-in Python functions. Other than this, the user can also use libraries like NumPy, which is a well developed library for performing operations on lists.

How do you apply an operation to a list in Python?

In Python, you can apply operations to lists using methods like for loops for simple operations, list comprehensions for concise code, and the map function for more complex operations. Libraries like NumPy and Pandas provide advanced tools for efficient list manipulation. Your choice depends on the operation's complexity and your coding style.

What is the fastest type of list in Python?

Lists with a time complexity of O(1) are considered to be the fastest, other than that, lists generated using external libraries like NumPy are considered to be highly efficient as they can store multidimensional arrays. 

Conclusion

In this article, we discussed Python list operations. We looked at various inbuilt functions, arithmetic, and logical operators applied on lists with their implementations. 

Recommended Readings:

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Happy learning, Ninja!

Previous article
What is reverse in python?
Next article
Difference between List, Tuple, Set and Dictionary in Python
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass