Methods for Lists in Python

List Python Data Structures are collections of objects and are mutable, have no fixed size. As lists are both mutable(can be changed) and have no fixed size(size can increase or decrease). That’s why there does exist many methods in Python which can specifically be applied. Let’s get straight into What list methods does Python have?
Below is a table containing List Method Names and Description. After the table, I’ve also put together some examples for each one of List Method so as to help you in better understanding these List Methods and how you can use these into your code.

List Methods in Python

List MethodDescription
append()Adds newer elements at end of list
clear()Removes all elements from list
copy()Returns a copy of list
count()Returns number of times some specified value occurs in a Python List
extend()Adds the elements of some list/iterable to end of specified list
index()Returns index of position where some specified value occurred first time
insert()Add an elements to some specified position in a list
pop()Removes element from specified position
remove()Removes first occurance of specified element from list
reverse()Reverses order of elements in list
sort()Sorts the list in Ascending Order

append() List Method in Python

Syntax : list.append()
This method can be used for adding values to list. While using this method just keep in mind that it add values to end of list.
Below are some code examples showing How to use append() List Method?

# Creating two Python Lists
list1 = [20, 28, 81, 192]
list2 = ["ComputerScienceHub", "Python Programming", "JavaScript Progamming"]

# Appending to list1
list1.append("78")
print(list1)         # Returns [20, 28, 81, 192, 78]

# Appending to list2
list2.append("Go Programming Language")
print(list2)        # Returns ["ComputerScienceHub", "Python Programming", "JavaScript Progamming", "Go Programming Language"]

clear() List Method in Python

Syntax : list.clear()
Removes all elements from list and turn it into an empty list. This method can be useful in scenarios where you need to hold some data as a temporary entity in your program.
Below are code examples showing How to use clear() method in Python?

# Creating a Python List
list1 = [9, 19, 124, 282]

# Clearing list
list1.clear()
print(list1)             # This will returns [] an empty Python List

copy() List Method in Python

Syntax : list.copy()
Makes a newer copy of a list. Please note here that .copy() method is not assigning a new reference to same memory location where list was stored. Rather it allocates some memory for newer list. For example – If we have list1 = [18, 29, 12] and doing list2 = list1.copy() will create a newer memory location for list2. So if in case any change is made to list1 then list2 would not change accordingly.
Below are code examples showing How to use copy() method in Python?

# Creating a Python List
list1 = [28, 91, 9]

# Copying list
list2 = list1.copy()
print(list2)            # Returns [28, 91, 9]

count() List Method in Python

Syntax : list.count(some string)
Returns number of times some specified value occurs in a Python List.

Below are code examples showing How to use count() method in Python?

# Creating a Python List
list1 = [18, 92, 192, 182, 163, 18, 64, 18, 362]

# Counting number of times it contain 18 value in it
list1.count(18)                # Returns 3

extend() List Method in Python

Syntax : list.extend(iterable object)
Adds the elements of some list/iterable to end of the specified list.

# Creating a Python List
list1 = [28, 91, 65, 73]

# Extending list
list1.extend([994, 283])
print(list1)             # Returns [28, 91, 65, 73, 994, 283]

index() List Method in Python

Syntax : list.index(number)
Returns index of position where some specified value occurred first time.

# Creating a Python List
list1 = [37, 29, 27]
list1.index(19)       # Returns 1 as number 29 is at index position 1 in list

insert() List Method in Python

Syntax : list.insert(index, value)
Add an elements to some specified position in a list
. This function can add some value at a specific index position meaning it would update index positions of other elements in list. For example – If for list1 = [19, 28, 17], we do list1.index(1, 373) then list1 will become [19, 373, 28, 17]. Here you can see clearly that position of 28 have shifted from being 1 to 2.
So keep in mind that insert() List Method can update index positions of some elements.

# Creating a Python List
list1 = [38, 29, 17]
list1.insert(1, 37)
print(list1)             # Returns [38, 37, 29, 17]

pop() List Method in Python

Syntax : list.pop(optional index)
Removes element from specified position if index is passed in, but if index is not passed in then removes element from end of list.

# Creating a Python List
list1 = [37, 91, 284, 29]

list1.pop()          # No index value is passed in, so removes last element from list
print(list1)         # Returns [37, 91, 284]

# Now list1 is [37, 91, 284]
list1.pop(1)         # Removes value at index position 1
print(list1)         # Returns [37, 284]

remove() List Method in Python

Syntax : list.remove(value)
Removes first occurance of specified element from list.
Please note here that if value is occurring more than once in list then remove() will only kick out first occurrence of value.

# Creating a Python List
list1 = [8, 92, 274, 28, 8, 92, 293]

list1.remove(8)         # Removes first occurrence of 8 in list1
print(list1)            # Returns [92, 274, 28, 8, 92, 293] note second 8 is still there

reverse() List Method in Python

Syntax : list.reverse()
Reverses order of elements in list.

# Creating a Python List
list1 = [38, 92, 182, 192]

list1.reverse()
print(list1)      # Returns [192, 182, 92, 38]

sort() List Method in Python

Syntax : list.sort()
Sorts the list in Ascending Order.

# Creating a Python List
site1 = [38, 19, 56, 24]

site1.sort()
print(site1)        # Returns [24, 56, 19, 38]

Methods for Some other Python Data Types

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts