×

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 Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

Gethostbyname() function in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

OSError in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

4 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 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 EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.