×

Python List Methods

Python List Methods

Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects:

Methods Explanation
append The list.append() method adds an item to the end of the list.
extend The list.extend() method extends the list by appending all the items from the iterable.
insert The list.insert() method adds an element at the specified position
remove The list.remove() method removes the first item with the specified value
pop The list.pop() method removes the element at the specified position of the list.
Clear The list.clear() method removes all the elements from the list
Index The list.index() method returns the index of the first element with the given list value.
Count The list.count() method returns the number of times x appears in the list.
Sort This method sorts the items of the list
Reverse This method reverses the elements of the list in place.
Copy The list.copy() method returns a copy of the specified list.

Example 1

 # Python program explaining
 # the list methods
 # list.append() method
 week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday", "Saturday"]
 print("List before calling append() method\n", week_list)
 # appending sunday at the end of the list
 week_list.append("Saturday")
 print("List after calling append() method")
 print(week_list)
 #list.extend() method
 str_list = ['The odd number', 'are:'] 
 num_list = [1, 3, 5, 7, 9] 
 str_list.extend(num_list) 
 print ("The extended list:",str_list)
 # list.remove() methods
 num_list.remove(5)
 num_list.remove(9)
 print("After removing the 5,9 values:",num_list) 

Output

 List before calling append() method
  ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
 List after calling append() method
 ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Saturday']
 The extended list: ['The odd number', 'are:', 1, 3, 5, 7, 9]
 After removing the values: [1, 3, 7] 

Example 2

 # Python program explaining
 # the list methods
 # the list.pop() method
 fruits = ['apple', 'banana', 'cherry']
 print("List: ",fruits)
 #removing the first element from the list
 fruits.pop(1)
 print("After removing the first element ")
 print("List: ",fruits)
 # list.index() method
 # printing the index of 'cherry' in fruits list  
 print("The index value of string value 'cherry': ",fruits.index('cherry'))
 # list.clear() method
 fruits.clear()
 print("After calling the clear() method..")
 print("fruits list:",fruits) 

Output

 List:  ['apple', 'banana', 'cherry']
 After removing the first element 
 List:  ['apple', 'cherry']
 The index value of string value 'cherry':  1
 After calling the clear() method..
 fruits list: [] 

Example 3

 # Python program explaining
 # the list methods
 # passing the alphabets list
 alphaList = ['b', 'a', 'e', 'd', 'c']
 print("Actual list: ",alphaList)
 # sort the alphabets list in the ascending order
 alphaList.sort()
 # priningt the list in a sorted order
 print('Sorted list:',alphaList)
 # list.reverse() method
 # reversing the order of the alpha list
 alphaList.reverse()
 print('Reversed list:',alphaList)
 # the list.copy() method
 # copying the the old list
 newList= alphaList.copy()
 print("New List: ", newList) 

Output

 Actual list:  ['b', 'a', 'e', 'd', 'c']
 Sorted list: ['a', 'b', 'c', 'd', 'e']
 Reversed list: ['e', 'd', 'c', 'b', 'a']
 New List:  ['e', 'd', 'c', 'b', 'a'] 

Related Topics

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Magento 2 Site optimization

Magento 2 Site optimization Magento 2 is a CMS, commonly referred to as Intensive efficiency. Improving the speed and reliability of your Magento 2 app helps clients to achieve a better user experience while...

2 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.