Python List Operations

In this section we are going to learn all the Python List Operations. There are many ways you can use list to manipulate the date in your program.

Python List Operations

Python List Operations explained

Python list is one of the collection data types in Python. In this tutorial we are going to explore Python List with the examples of various operations that we can perform on the list object in Python. A Python list is used to store many values within a one variable. Lists are ordered, and allow duplicates values. There are many operations that can be performed on the list which helps developers in developing powerful data management applications in core Python.

In this Python List tutorial I will show examples of following operations:

  1. Creating a list
  2. Accessing elements
  3. Slicing
  4. Adding elements
  5. Removing elements
  6. Removes last element and returns the list
  7. Concatenating lists
  8. Sorting
  9. Reversing
  10. Finding the length
  11. Checking if an element exists

Python List Operations

We have recorded the video instructions that will show you how to use these operations on the list objects.

Here is complete code described in this tutorial:

# Creating a list
my_list = [1, 2, 3, 4, 5]

# Accessing elements
print(my_list[0])  # Output: 1
print(my_list[-1]) # Output: 5

# Slicing
print(my_list[1:3])  # Output: [2, 3]

# Adding elements
my_list.append(6)  # Adds 6 to the end
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

my_list.insert(2, 7)  # Inserts 7 at index 2
print(my_list)  # Output: [1, 2, 7, 3, 4, 5, 6]

# Removing elements
my_list.remove(3)  # Removes the first occurrence of 3
print(my_list)  # Output: [1, 2, 7, 4, 5, 6]

my_list.pop()  # Removes last element and returns the list
print(my_list)  # Output: [1, 2, 7, 4, 5]

# Concatenating lists
my_list2 = [7, 8, 9]
my_list.extend(my_list2)  # Adds elements of my_list2 to my_list
print(my_list)  # Output: [1, 2, 7, 4, 5, 7, 8, 9]

# Sorting
my_list.sort()  # Sorts the list in ascending order
print(my_list)  # Output: [1, 2, 4, 5, 7, 7, 8, 9]

# Reversing
my_list.reverse()  # Reverses the order of elements
print(my_list)  # Output: [9, 8, 7, 7, 5, 4, 2, 1]

# Finding the length
print(len(my_list))  # Output: 8

# Checking if an element exists
print(5 in my_list)  # Output: True

You run above examples in the Python interpreter to see the output of the above code.

Let's understand all the list operations with the examples.

1. Creating a list

List is one of the data types in the Python which is used to store multiple items in a single variable. To create a blank list you can use angle brackets. To create empty list you don't have to provide any element inside the angle bracket []. Here is an example of creating an empty list:


# Creating empty list
my_emppty_list = []

To create list by initializing the data you can provide values inside the angle brackets e.g  [1,2,3,4]

Example code of Creating a list with the values:


# Creating a list
my_list = [1, 2, 3, 4, 5]

2. Accessing elements

In Python you can access the item by passing the index of the item. The index of the list starts from 0 (zero), the first item's index is 0 in the list. If you pass -1 then the last value in the list is returned.

Here is the example code of Accessing elements from list:


# Accessing elements
print(my_list[0])  # Output: 1
print(my_list[-1]) # Output: 5

3. Slicing List

In Python Slicing the list is the process of accessing a specific range or subset of a list while leaving the rest of the items in the list. The syntax of the slicing list is [start:stop:step], where start is the start index and stop is the stopping index. The step allows the user to select the nth item within the start and end range.

Example code of Slicing a list:


# Slicing
print(my_list[1:3])  # Output: [2, 3]

4. Adding elements

The append() function of list allows the users to add an item at the end of the list. Here is the example code of Adding elements to the list


# Adding elements
my_list.append(6)  # Adds 6 to the end
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]
my_list.insert(2, 7)  # Inserts 7 at index 2
print(my_list)  # Output: [1, 2, 7, 3, 4, 5, 6]

5. Removing elements

The remove() method is used to remove the first occurrence of the given item from the list.

Following is the example code of Removing elements from string


# Removing elements
my_list.remove(3)  # Removes the first occurrence of 3
print(my_list)  # Output: [1, 2, 7, 4, 5, 6]

6. Removing last element and returns the list

The pop() is used to remove the item at the specific index. If no index is provided to the pop() method then it removes the item from the end of the list. This code removes last element and returns the list:


my_list.pop()  # Removes last element and returns the list
print(my_list)  # Output: [1, 2, 7, 4, 5]

7. Concatenating lists

For concating two lists we will use the extend() function of the list. The extend() method is used to add all the elements from the given iterable object (list, tuple, dictionary, or string) at the end of the list. Here is the example code of Concatenating lists


# Concatenating lists
my_list2 = [7, 8, 9]
my_list.extend(my_list2)  # Adds elements of my_list2 to my_list
print(my_list)  # Output: [1, 2, 7, 4, 5, 7, 8, 9]

8. Sorting

The sort() method is used to sort the list. Here is the example code of Sorting a list:


# Sorting
my_list.sort()  # Sorts the list in ascending order
print(my_list)  # Output: [1, 2, 4, 5, 7, 7, 8, 9]

9. Reversing

The reverse() method is used to reverse a list. Here is the example code of Reversing a list:


# Reversing
my_list.reverse()  # Reverses the order of elements
print(my_list)  # Output: [9, 8, 7, 7, 5, 4, 2, 1]

10. Finding the length

The len() function will return the number of items in the list. Following example code can be used for finding the length of list:


# Finding the length
print(len(my_list))  # Output: 8

11. Checking if an element exists

You can even check if a item with the specific exists in the list. Here is the example code of Checking if an element exists:


# Checking if an element exists
print(5 in my_list)  # Output: True

In this tutorial we have explored Python list operations with many examples. We learned to create a list with some items and then used the append() method to add more items to the list. After adding more items to the list we showed you various operations that you can perform on the list.

Related Tutorials