×

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

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.