×

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 program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Joint Plot in Python

Introduction to Joint plots: The joint plot is the finest approach to evaluate both the specific distribution of each variable and also the connection between the two variables. Three different plots make...

4 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Add a key-value pair to dictionary in Python

In programming, data type defines the type of value that a variable can hold. With help of these, we can perform various mathematical, logical, or relational operations on that particular...

5 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Hog Descriptor Opencv 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.

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.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 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 Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.