What are List Methods in Python

Read it in 7 Mins

Last updated on
24th Feb, 2022
Published
04th Nov, 2019
Views
5,480
What are List Methods in Python

Sequence is one of the most basic data types in Python. Every element of a sequence is allocated a unique number called its position or index. The first designated index is zero, the second index is one, and so forth. Although Python comes with six types of pre-installed sequences, the most used ones are lists and tuples, and in this article, we would be discussing lists and their methods.

Certain tasks can be performed with all types of sequence; these include adding, multiplying, indexing, slicing, and so on. For added convenience, Python provides built-in functions to find the length of a sequence and to find the largest and smallest elements of the sequence. If you are interested to learn more about other functions and features of Python, you may go through our Knowledgehut python programming certification or free the Python tutorial.

What is a List

List method in Python is the most versatile data-type available in Python that can be written as a collection of comma-separated values or items between square brackets. The items in a list need not necessarily be homogeneous, i.e. of the same type. This property of List makes it one of the most powerful tools in Python eg:[‘HELLO’, 57, “SKY”]. A single list can contain different Data-Types such as integers, strings, as well as Objects. Lists are mutable, and hence can be changed even after their creation.

In Python, lists are ordered in a definite manner and have a definite count. Elements of a list are indexed according to a particular sequence and the indexing is done with 0 being the starting index. Even element of a list has its unique place in the list, which allows duplication of the elements in the list, with each element having its own distinct place and credibility. Lists are a useful tool for storing a sequence of data.

Creating a list is as simple as putting different types of comma-separated values between square brackets:

list1 = ['jack', 'jill', 1998, 2019];[Text Wrapping Break] list2 = [1, 2,
 3, 4, 5 ];[Text Wrapping Break] list3 = ["w", "x", "y", "z"]

Just like string indices, list indices start with 0, and lists can be sliced, concatenated and so on.

Creating a List

Python Lists can be created just by placing the integer, string, or character inside the square brackets[]. Unlike Sets, a list does not require a built-in function for its creation.

# Python program to demonstrate the creation of a list
[Text Wrapping Break] # Creating a List [Text Wrapping Break] List = []
[Text Wrapping Break] print("Initial blank List: ")
[Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #
Creating a List with  [Text Wrapping Break] # the use of a String
[Text Wrapping Break] List = ['PythonListDemo']
[Text Wrapping Break] print("\nList with the use of String: ")
[Text Wrapping Break] print(List) [Text Wrapping Break][Text Wrapping Break] # 
Creating a List with [Text Wrapping Break] # the use of multiple values 
[Text Wrapping Break] List = ["Python""List""Demo"]
[Text Wrapping Break] print("\nList containing multiple values: ")
[Text Wrapping Break] print(List[0]) [Text Wrapping Break] print(List[2])
[Text Wrapping Break] [Text Wrapping Break] # Creating a Multi-Dimensional List
[Text Wrapping Break] # (By Nesting a list inside a List) [Text Wrapping Break] List = [['Python', 'List'] , ['Demo']]
[Text Wrapping Break] print("\nMulti-Dimensional List: ")
[Text Wrapping Break] print(List)
Initial blank List:
[Text Wrapping Break]
[] [Text Wrapping Break] [Text Wrapping Break]
List with the use of String:
[Text Wrapping Break]
['PythonListDemo'] [Text Wrapping Break]
[Text Wrapping Break] List 
containing multiple values:
[Text Wrapping Break] Python [Text Wrapping Break]
Demo[Text Wrapping Break] [Text Wrapping Break]
Multi-Dimensional List: 
[Text Wrapping Break] [['Python',
'List'], ['Demo']]

Creating a list with multiple distinct or duplicate elements:

Multiple distinct or duplicate values can be stored as a sequence during creation of list:

# Creating a List with [Text Wrapping Break] # the use of 
Numbers [Text Wrapping Break] # (Having duplicate values)
[Text Wrapping Break] List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
[Text Wrapping Break] print("\nList with the use of Numbers: "[Text Wrapping Break] print(List) [Text Wrapping Break] 
[Text Wrapping Break] # Creating a List with
[Text Wrapping Break] # mixed type of values [Text Wrapping Break] # 
(Having numbers and strings) [Text Wrapping Break] List = [1,
2, 'Python', 4, 'List', 6, 'Demo']
[Text Wrapping Break] print("\nList with the use of Mixed
Values: ") [Text Wrapping Break] print(List)
List with the use of Numbers: [Text Wrapping Break][1, 2, 4, 4, 3, 3, 3, 6, 
5] [Text Wrapping Break] [Text Wrapping Break] List with the use of Mixed Values:
[Text Wrapping Break] [1, 2, 'Python', 4, 'List', 6, 'Demo'] [Text Wrapping Break]

Adding Elements to a List

Using append() method

Using the built-in append() function elements can be added to the List. At a time only one element can be added to the list by the use of append() method, but for the addition of multiple elements with the method, loops are used. Unlike Sets, a new List can be added to an existing one with the use of the append() method.

# Python program to demonstrate addition of elements in a List
[Text Wrapping Break] [Text Wrapping Break] # Creating a List
[Text Wrapping Break] List = [] [Text Wrapping Break] print("Initial blank List: ")
[Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # 
Addition of Elements  [Text Wrapping Break] # in the List
[Text Wrapping Break] List.append(1) [Text Wrapping Break] List.append (2)
[Text Wrapping Break] List.append(4) [Text Wrapping Break] print("\nList after
Addition of Three elements: ") [Text Wrapping Break] print(List)
[Text Wrapping Break] [Text Wrapping Break]# Adding elements to the List
[Text Wrapping Break] # using Iterator [Text Wrapping Break] for i in range(1, 4):
[Text Wrapping Break] List.append(i) [Text Wrapping Break] print("\nList after
Addition of elements from 1-3: ") [Text Wrapping Break] print(List)
[Text Wrapping Break] [Text Wrapping Break] # Addition of List to a List
[Text Wrapping Break] List2 = ['Python', 'List']
[Text Wrapping Break] List.append(List2) [Text Wrapping Break] print ("\nList after
Addition of a List: ") [Text Wrapping Break] print(List)
Initial blank List: [Text Wrapping Break] [] [Text Wrapping Break] 
[Text Wrapping Break] List after Addition of Three elements: [Text Wrapping Break]
[1,2,4][Text Wrapping Break][Text Wrapping Break]List after Addition of
elements from 1-3[Text Wrapping Break] [1, 2, 4, 1, 2, 3[Text Wrapping Break] 
[Text Wrapping Break] List after Addition of a List:[Text Wrapping Break] [1, 2, 
4, 1, 2, 3, ['Python', 'List']]

Using insert() method

Append() method restricts the addition of elements at the end of the List only. Using the insert() method, elements can be added to the list at your desired position. Unlike append() which requires only one argument, insert() method requires two arguments for defining the position and value of the element to be inserted (position, value).

# Python program to demonstrate addition of elements in a List
[Text Wrapping Break] [Text Wrapping Break] # Creating a List 
[Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List: 
") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #
Addition of Element at  [Text Wrapping Break] # specific Position
[Text Wrapping Break] # (using Insert Method) [Text Wrapping Break] List.insert(3,
12) [Text Wrapping Break] List.insert(0, 'Python')
[Text Wrapping Break] print("\nList after performing Insert Operation: ")
[Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break]
[Text Wrapping Break] List after performing Insert Operation:
[Text Wrapping Break] [ 'Python', 1, 2, 3, 12, 4]

Using extend() method

Apart from append() and insert() methods, there’s another method to add elements by the use of extend() method. This method is used for adding multiple elements to the end of the list at once.

# Python program to demonstrate  [Text Wrapping Break] # Addition of elements
in a List [Text Wrapping Break] [Text Wrapping Break] # Creating a List 
[Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List:
") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # 
Addition of multiple elements [Text Wrapping Break] # to the List at the end
[Text Wrapping Break] # (using Extend Method) 
[Text Wrapping Break] List.extend ( [8,
 'Python', 'Program']) [Text Wrapping Break] print( "\nList after performing 
Extend Operation: " ) [Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break] 
[Text Wrapping Break] List after performing Extend Operation: 
[Text Wrapping Break] [1, 2, 3, 4, 8, 'Python', 'Program']

Accessing elements from the List

In order to access the items in a List, the index number is used as reference. The index operator [ ] is used to access the items of a list. The index should be an integer and nested lists are accessed by using nested indexing.

# Python program to demonstrate  [Text Wrapping Break] # accessing of element
from list [Text Wrapping Break][Text Wrapping Break] # Creating a List with
[Text Wrapping Break]# the use of multiple values [Text Wrapping Break] List =
["Access", "List", "Elements"] [Text Wrapping Break]  [Text Wrapping Break]#
accessing an element from the  [Text Wrapping Break] # list using index number
[Text Wrapping Break] print("Accessing an element from the list")
[Text Wrapping Break] print(List[0])  [Text Wrapping Break] print(List[2])
[Text Wrapping Break]  [Text Wrapping Break] # Creating a Multi-Dimensional List
[Text Wrapping Break] # (By Nesting a list inside a List)
[Text Wrapping Break] List = [['Access', 'List'] , ['Elements']]
[Text Wrapping Break]  [Text Wrapping Break] # accessing an element from the
[Text Wrapping Break] # Multi-Dimensional List using [Text Wrapping Break] # index
number [Text Wrapping Break] print("Accessing an element from a Multi - 
Dimensional list") [Text Wrapping Break] print(List[0][1])
[Text Wrapping Break] print(List[1][0])
Accessing an element from the
list [Text Wrapping Break] Access [Text Wrapping Break] Elements [Text Wrapping Break] [Text Wrapping Break]
Accessing an element from a Multi-Dimensional
 list [Text Wrapping Break] List [Text Wrapping Break] Elements

Negative indexing

In Python, negative sequence indexing means the representation of positions of the array from the end. Rather than calculating the offset like List[len(List)-3], we can just write it like List[-3]. Here, -1 refers to the last item, -2 refers to the second last item etc. i.e. beginning from the end.

List = [1, 2, 'Python', 4, 'Negative', 6, 'Index'] [Text Wrapping Break]
[Text Wrapping Break] # Accessing an element using negative indexing
[Text Wrapping Break] print("Accessing element using negative indexing")
[Text Wrapping Break]  [Text Wrapping Break] # print the last element of list
[Text Wrapping Break] print(List[-1]) [Text Wrapping Break] [Text Wrapping Break]#
print the third last element of list  [Text Wrapping Break] print(List[-3])
Accessing element using negative
indexing [Text Wrapping Break] Index [Text Wrapping Break] Negative

Take a look at the python full course advanced and learn more about list operations in Python and work on real projects.

Removing Elements from the List

Using remove() method

In Python, using the built-in remove() function, elements can be removed from the List but an Error will arise if the element is not present in the set. Remove() method is only capable of removing one element at a time, to remove a range of elements, an iterator is used. A limitation of this method is that it will only remove the first occurrence of the searched element and would not work if there are multiple occurrences of the searched element.

# Python program to demonstrate removal of elements in a List
[Text Wrapping Break][Text Wrapping Break] # Creating a List
[Text Wrapping Break] List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List
[Text Wrapping Break] # using Remove() method [Text Wrapping Break] List.remove(5)
[Text Wrapping Break] List.remove(6) [Text Wrapping Break] print("\nList after
removal of two elements: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List
[Text Wrapping Break] # using iterator method [Text Wrapping Break] for i in
range(1, 5): [Text Wrapping Break]    List.remove(i)
[Text Wrapping Break] print("\nList after removing a range of elements: ")
[Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[Text Wrapping Break][Text Wrapping Break]List after removal of two elements:
[Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12][Text Wrapping Break]
[Text Wrapping Break] List after removing a range of elements:
[Text Wrapping Break] [7, 8, 9, 10, 11, 12]

Using pop() method

In Python,  we can also remove and return an element from the set using the Pop() function, but it removes  the last element of the set only by default. To remove a specific element from a position of the List, index of the element is passed as an argument to the pop() function.

List = [1,2,3,4,5] [Text Wrapping Break] [Text Wrapping Break] # Removing element
from the  [Text Wrapping Break] # Set using the pop() method
[Text Wrapping Break] List.pop() [Text Wrapping Break] print("\nList after popping
an element: ") [Text Wrapping Break] print(List) [Text Wrapping Break] 
[Text Wrapping Break] # Removing element at a  [Text Wrapping Break] # specific
location from the  [Text Wrapping Break] # Set using the pop() method
[Text Wrapping Break] List.pop(2) [Text Wrapping Break] print("\nList after popping
a specific element: ") [Text Wrapping Break] print(List)
List after popping an element: [Text Wrapping Break] [1, 2, 3, 4[Text Wrapping Break][Text Wrapping Break]List after popping a specific 
element: [Text Wrapping Break][1, 2, 4]

Slicing of a List

Although there are several ways to print the whole List with all the elements in Python, there is only one way to print a specific range of elements from the list: by the use of Slice operation. Slice operation is performed on Lists by the use of colon(:). For printing elements from the beginning of the range use [:Index], for printing elements from end use [:-Index], to print elements from a specific index till the end use [Index:], for printing elements within a specific range, use [Start Index: End Index] and to print the entire List by the use of slicing operation, use [:]. Moreover, in order to print entire List in reverse order, use [::-1]. For printing the elements of List from rear end, negative indexes are used.

# Python program to demonstrate removal of elements in a List
[Text Wrapping Break][Text Wrapping Break]# Creating a List
[Text Wrapping Break] List =
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Print elements of a range
[Text Wrapping Break] # using Slice operation [Text Wrapping Break] Sliced_List =
List[3:10] [Text Wrapping Break] print("\nSlicing elements in a range 3-10: ")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
Print elements from a  [Text Wrapping Break] # pre-defined point to end
[Text Wrapping Break] Sliced_List = List[6:]
[Text Wrapping Break] print("\nElements sliced from 6th ""element till the
end: ") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]
[Text Wrapping Break]# Printing elements from [Text Wrapping Break] # beginning
till end [Text Wrapping Break] Sliced_List = List[:]
[Text Wrapping Break] print("\nPrinting all elements using slice operation: ")
[Text Wrapping Break] print(Sliced_List) 
Initial List: [Text Wrapping Break]
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'[Text Wrapping Break]
[Text Wrapping Break] Slicing elements in a range 3-10: [Text Wrapping Break]['H',
'O', 'N', 'P', 'R','O','G'][Text Wrapping Break][Text Wrapping Break]Elements
sliced from 6th element till the end: [Text Wrapping Break]['P', 'R', 'O',
'G', 'R', 'A', 'M'][Text Wrapping Break][Text Wrapping Break]Printing all
elements using slice operation: [Text Wrapping Break] ['P', 'Y', 'T', 'H',
'O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']

Negative index List Slicing

# Creating a List [Text Wrapping Break] List =
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Print elements from beginning
[Text Wrapping Break] # to a pre-defined point using Slice
[Text Wrapping Break] Sliced_List = List[:-7]
[Text Wrapping Break] print("\nElements sliced till 7th element from last: ")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
 Print elements of a range [Text Wrapping Break] # using negative index List
slicing [Text Wrapping Break] Sliced_List = List[-6:-1]
[Text Wrapping Break] print("\nElements sliced from index -6 to -1")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
 Printing elements in reverse [Text Wrapping Break] # using Slice operation
[Text Wrapping Break] Sliced_List = List[::-1]
[Text Wrapping Break] print("\nPrinting List in reverse: ")
[Text Wrapping Break] print(Sliced_List)
Initial List: [Text Wrapping Break]
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'][Text Wrapping Break]
[Text Wrapping Break]Elements sliced till 7th element from last:
[Text Wrapping Break]['P', 'Y', 'T', 'H', 'O', 'N'][Text Wrapping Break]
[Text Wrapping Break]Elements sliced from index -6 to -1[Text Wrapping Break]
['R', 'O', 'G', 'R', 'A'][Text Wrapping Break] [Text Wrapping Break]Printing List
in reverse:[Text Wrapping Break]['M', 'A', 'R', 'G', 'O', 'R', 'P', 'N',
'O', 'H', 'T', 'Y', 'P']

Updating Lists

You can update single or multiple elements of lists by reassigning the values on the lists individually.

# Python program to update elements of a list[Text Wrapping Break]list =
['physics', 'chemistry', 1998, 2019];[Text Wrapping Break] print "Value
available at index 2 : "[Text Wrapping Break] print list[2]
[Text Wrapping Break] list[2] = 2000;[Text Wrapping Break] print "New value
available at index 2 : "[Text Wrapping Break] print list[2]
Value available at index 2 : [Text Wrapping Break] 1998 [Text Wrapping Break] New
value available at index 2 : [Text Wrapping Break] 2000

Built-in functions

FUNCTION DESCRIPTION

sum() Addsall numbers.

ord() Used for returning an integer which represents the unique Unicode code point of the given Unicode character.

cmp() If the first list is “greater” than the second list, the function returns 1.

max() It returns the largest element in the list.

min() It returns the smallest element in the list.

all() It returns true if all elements are true or false if any element in the list is empty.

any() It returns true if even one of the elements of the list is true. If one list is empty, it returns false.

len() It returns length the list.

enumerate() It adds a counter to an enumerate object that can be used directly for loops.

accumulate() It makes an iterator that gives the result of a function. It takes a function as an argument.

filter() It can individually check if every element is true or not.

map() It applies a particular function to each item of an iterable and shows a list of the results.

lambda() It is an anonymous function that behaves like a normal function in regard to arguments. While normal functions are defined with def keyword, anonymous functions are defined using lambda keyword.

List Methods

FUNCTION DESCRIPTION

Append() Adds an element at the end of the list

Extend() Adds all elements of one list to another list

Insert() Inserts an item at a desired index

Remove() Removes an item from the list

Pop() Removes and returns an element at a desired index

Clear() Removes all elements from the list

Index() Returns the index of the first identical item

Count() Returns the number of items passed as argument

Sort() Sort items of a list in ascending order

Reverse() Reverses the list

copy() Returns a copy of the list

Summary

In this article, we have covered the concept of Lists in Python. You have learned the basics of creating a List, adding value to it, accessing its elements, removing the elements, and various other operations. We have also covered some basic built-in functions of Python and several other methods along with their functions. To gain more knowledge about Python tips and tricks, check our Python tutorial and get a good hold over coding in Python by joining the python programming certification.

Profile

Priyankur Sarkar

Data Science Enthusiast

Priyankur Sarkar loves to play with data and get insightful results out of it, then turn those data insights and results in business growth. He is an electronics engineer with a versatile experience as an individual contributor and leading teams, and has actively worked towards building Machine Learning capabilities for organizations.