Lists

List Functions and Methods

In Python, we can use any list function which makes a collection that we can manipulate for our analysis. Moreover, this collection of data is known as a list object. However, all the methods are functions but not all the functions are methods in Python. This article will focus on the list functions and methods to get a better understanding of the same.

The Python list methods are as follows:

append()

It adds a single element to the end of the list

Example: Adding an element to the List:

                    
# animals list

animals = ['cat', 'monkey', 'rabbit']

# 'guinea pig' is appended to the animals' list

animals.append('guinea pig')

# Updated animals list

print('Updated animals list: ', animals)

 

Output

                    
Updated animals list:  ['cat', 'monkey', 'rabbit', 'guinea pig']

clear()

 

It removes all the items from the List

Example: How the clear() method works:

# Defining a list

                    
list = [{1, 3}, ('a'), ['1.1', '3.3']]

# clearing the list

list.clear()

print('List:', list)

Output

List: []

copy()

 

It returns a shallow copy of the given list

Example:

                    
old_list = [1, 2, 5]

new_list = old_list

# add an element to list

new_list.append('a')

print('New List:', new_list)

print('Old List:', old_list)

 

Output

                    
Old List: [1, 2, 5, 'a']

New List: [1, 2, 5, 'a']

 

count()

It returns the count of the elements present in the list

Example: the use of count() is as follows:

                    
# vowels list

vowels = ['a', 'e', 'i', 'o', 'a', 'u']

# count element 'a'

count = vowels.count('a')

# print count

print('The count of a is:', count)

# count element 'p'

count = vowels.count('p')

# print count

print('The count of p is:', count)

 

Output

                    
The count of ‘a’ is: 2

The count of ‘p’ is: 0

 

extend()

It adds the iterable elements to the ending of the list

Browse more Topics under Lists

Example: the use of extend() Method is as follows:

                    
# languages list

languages = ['French', 'German']

# another list of language

languages1 = ['Spanish', 'Portuguese']

# appending language1 elements to language

languages.extend(languages1)

print('Languages List:', languages)

 

Output

Languages List: [‘French’, ‘German’, ‘Spanish’, ‘Portuguese’]

index()

It returns the index of the elements in the given list.

Example: Find out the index of the given element

                    
# vowels list

vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of 'a' in vowels

index = vowels.index('a')

print('The index of a:', index)

# element 'i' is searched

# index of the first 'i' is returned

index = vowels.index('i')

print('The index of i:', index)

 

Output

                    
The index of ‘a’: 1

The index of ‘i’: 2

 

insert()

It inserts an element to the given list

Example: Inserting an Element to the present List:

                    
# vowel list

vowel = ['a', 'e', 'i', 'o']

# 'u' is inserted at index 4

# the position of 'u' will be 5th

vowel.insert(4, 'u')

print('Updated List:', vowel)

Output

Updated List: ['a', 'e', 'i', 'o', 'u']

 

pop()

It removes an element at the provided index

Example: Pop item at the provided index from the given list:

                    
# programming languages list

languages = ['Python', 'Java', 'C++', 'German', 'C']

# remove and return the 4th item

return_value = languages.pop(3)

print('Return Value:', return_value)

# Updated List

print('Updated List:', languages)

 

Output

Updated List: [‘Python’, ‘Java’, ‘C++’, ‘C’]

remove()

It removes the item from the given list:

Example: Removing an element from the given list:

                    
# animals list

animals = ['cat', 'dog', 'mouse', 'guinea pig']

# 'mouse' is removed

animals.remove('mouse')

# Updated animals List

print('Updated animals list: ', animals)

 

Output

Updated animals list:  [‘cat’, ‘dog’, ‘guinea pig’]

reverse()

It reverses the given list

Example: Reversing a list:

                    
# Operating System List

systems = ['Linux', 'macOS', 'Windows']

print('Original List:', systems)

# List Reverse

systems.reverse()

# updated list

print('Updated List:', systems)

 

Output

                    
Original List: ['Linux', 'macOS', 'Windows']

Updated List: ['Windows', 'macOS', 'Linux']

 

sort()

It sorts the elements of the given list:

Example: Sorting a given list:

                    
# vowels list

vowels = ['a', 'e', 'u', 'o', 'i']

# sort the vowels

vowels.sort()

# print vowels

print('Sorted list:', vowels)

Output

Sorted list: ['a', 'e', 'i', 'o', 'u']

 

FAQs on List Functions and Methods

Question 1: How do we list all the functions?

Answer: We have to type ‘module_name’, press tab. Moreover, it’ll then open a small window having the list of all the functions in the python module.

Question 2: How to make a list?

Answer: We can create a list by placing all the available items (elements) inside the square brackets ‘[]’ , which we have to separate with commas.

Share with friends
Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started
Customize your course in 30 seconds

Which class are you in?

No thanks.