List Methods in Python

In this exercise, we will learn about the List methods in Python.

a) list.sort() method The sort method sorts the list in ascending order by default. The original list is updated. If we want to sort the list in descending order, we must give reverse=True as a parameter in the sort method. The reverse parameter is set to False by default.

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange"]

# sort of list in ascending order, original list itself is modified
my_list.sort()

# Print the list
print(my_list)
# ['Blue', 'Green', 'Orange', 'Red']

num = [2,7,86,3,1]

# sort of list in ascending order, original list itself is modified
num.sort()

# Print the list
print(num)
# [1, 2, 3, 7, 86]

# sort of list in descending order, original list itself is modified
num.sort(reverse=True)

# Print the list
print(num)
# [86, 7, 3, 2, 1] 

b) list.reverse() method The reverse() method is used to reverse the order of items in the list.

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange"]

# Reverse the list, original list itself is modified
my_list.reverse()

# Print the list
print(my_list)
# ['Orange', 'Blue', 'Green', 'Red']

# Create a list
num = [2,7,86,3,1]

# Reverse the list, original list itself is modified
num.reverse()

# Print the list
print(num)
# [1, 3, 86, 7, 2] 

c) list.index() The index() method returns the index of the first occurrence of the specified list item.

Syntax: insert(index, value)

Example:

Python

colors = ["voilet", "green", "indigo", "blue", "green"]
print(colors.index("green"))

num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
print(num.index(3)) 

Output 1
3

d) list.count() The count() method returns the count of the number of items with the given value.

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange", "Green"]

# Count specified item in the list
count = my_list.count("Green")

# print the count
print(count) 

e) copy() The copy() method returns a shallow copy of the list. This can be done to perform operations on the list without modifying the original list.

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange", "Green"]

# Create a variable that references the list
reference_list = my_list

# Change the index 0 value in the referenced list
reference_list[0] = "Yellow"

# Print the original list
print(my_list)
# ['Yellow', 'Green', 'Blue', 'Orange', 'Green'] 

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange", "Green"]

# Create a variable that has a copy of the list
new_list = my_list.copy()

# Change the index 0 value in the new list
new_list[0] = "Yellow"

# Print the original list
print(my_list)
# ['Red', 'Green', 'Blue', 'Orange', 'Green']

# Print the new list
print(new_list)
# ['Yellow', 'Green', 'Blue', 'Orange', 'Green'] 

f) append() method The append() method appends items to the end of the existing list.

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", "Orange"]

# Append item in the list, original list itself is modified
my_list.append("Yellow")

# Print the list
print(my_list)
# ['Red', 'Green', 'Blue', 'Orange', 'Yellow'] 

g) list.insert() method The insert() method inserts an item at the given index. User must specify index and the item to be inserted within the insert() method.

Example:

Python

colors = ["voilet", "indigo", "blue"]
#           [0]        [1]      [2]

colors.insert(1, "green")   #inserts item at index 1
# updated list: colors = ["voilet", "green", "indigo", "blue"]
# indexes                   [0]       [1]      [2]       [3]

print(colors) 

Output ['voilet', 'green', 'indigo', 'blue']

h) list.extend() method The extend() method adds an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.

Example:

Python

# Add a list to a list
colors = ["voilet", "indigo", "blue"]
rainbow = ["green", "yellow", "orange", "red"]
colors.extend(rainbow)
print(colors) 

Output ['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']

i) remove method of List The remove() method is used to remove the first occurrence of a value.

Example:

Python

# Create a list of numbers
numbers = [45, 76, 34, 23, 20, 48, 20]

numbers.remove(20)
print(numbers)
# [45, 76, 34, 23, 48, 20] 

j) list.pop() method We can use pop() method to remove an item at the given index and return its value.

The pop() method removes and returns the last item if the index is not provided. This helps us implement lists as stacks (first in, last out data structure).

Example:

Python

# Create a list of numbers
numbers = [45, 76, 34, 23, 20, 48, 20]

# Remove item at index 3 (which is the number 23) from the list
val = numbers.pop(3)

print(numbers)
# [45, 76, 34, 20, 48, 20]

print(val)
# 23 

k) list.clear() method The list.clear() method removes all items from the list We can also use the clear() method to empty a list.

Example:

Python

# Create a list of numbers
numbers = [45, 76, 34, 23, 20, 48, 20]

# Clears the list
numbers.clear()

print(numbers)
# [] 

l) len() method The len method is used to find the length of the list, it means number of items in the list.

Syntax: len (list_name)

Example:

Python

# Create a list
my_list = ["Red", "Green", "Blue", 7]

# length of list
print(len(my_list))