Python list methods and List Operations

List Operations:

Since List is a Python s data type, we can perform several operations on the data represented as List.The operations commonly performed on List are combining two or more Lists, replicating the List and Slicing the List.

Combining the Lists:

1. Also known as joining the List, combining the List is simply adding two or more List to create a new. concatenation operator ‘+’ is used to join two Lists.

2. The ‘+’ operator requires that both the operands must be of List types. Thus we cannot add a number or string to a List. We can create List by adding two or more Lists.

Syntax : <list 1>+<list 2>

Example : print(mrk 1+mrks 2)

Q) Write a program to create two lists A and B with the names of 5-5 students. add both the Lists into one and finally print the combined List.
A= [‘Anupama’, ‘Bankim’, ‘Chetan’, ‘Disha’, ‘Eric’
B= [‘Ebrahim’, ‘Dinesh’, ‘Chandni’, ‘Beena’, ‘Arun’]
print(“\nFirst List: “, A)
print(“\nSecond List: “, B)
print(“\nFinal List: “, C)

Replicating the List

1. The arithmetic operator of multiplication ‘*’ is used to replicate the Lists upto specified number times.

2. The ‘*’ operator requires that one operand must be of List types and the other must be an integer. We can create a new List by replicating a List up to a specified number of times.

Syntax : <list name> * <integer>
Example : print(marks*3)

The above statement will print a new list by replicating elements of List ‘marks’ three times.

Q) The following program demonstrates the use of replicating operator with List.
print(“\nNew List:”, newnum)

Slicing the List

Slicing the List is the process of extracting some part of the List out of it. In other words, it is the process of creating sub-list from a main List. We use indexes of the List elements to create a slice

<list name>[start: stop: step]

Here <list name> is name of the List to be sliced. <start> represent the index from where extraction is to begin. The default of <start> is 0(zero). <stop> is the index numbers up to which extraction will take place. Actually extraction takes place up to <stop>-1 i.e. element at position <stop> index is not included in slice. The default of <stop> is the end of List. <step> is used when all the numbers between <start> and <stop> are not to be sliced. <step> represent the number by

num=[11, 22, 33, 44, 55, 66, 77, 88, 99]


The above statement will slice the List by extracting elements

from index positions 1, 3 and 5, so the output will be [22,44,66]

 The following program will demonstrate the slicing process of a given List.

num=[11, 22, 33, 44, 55, 66, 77, 88, 99]
print(“\nOriginal List: “, num)
print(“\nSlice [1:7:3]=”, num[1:7:3])
num1 = num[2:6]
print(“\nSlice [2:6]=”, num1)
print(“\nSlice [4:]=”, num[4:])
print(“\nSlice [:6]=”, num2)
print(“\nSlice [::]=”, num[::])

Adding elements to List

if a List is already created, we can add a new element in it. The append() method is used to add single element to the end of list.


<list name>.append(<element>)

 Write a program to create a list of 10 numbers. Numbers should be enter by the user.

for a in range(10):
k=int(input(“Enter a number”))
print(“The List is: “, num)

 Updating the Element

Since List is a mutable data type of Python, it is possible to change or update its elements place. To update or change an existing element of List we assign a new element at the same index position of the List where the old element is present, this way the old get replaced by new


<list name>[<index>]=<element>

Write a program to create a list of 5 names. The program should ask user to input a new name and index position to update the element with new name

name=”Aditi Bhawna’, ‘Chetna’, ‘Dipika’, ‘Enabel’]
print(“The current List is: \n”, name)
k=input(“\nEnter a new name:”)
pos=int(input(“Enter the index to replace element: “))
print(“\nThe updated List = \n”, name)

 Deleting Elements

We can also remove elements from the List. The ‘del’ statement of Python is used to delete an individual element from the List or any sub-set of elements defined by slice.


del<list name>[<index>]
del name[3]

This example will delete the element from index position 3. Since index position starts with 0, it will actually delete element number 4.


del <list name>[<start>: <stop>]
del name[3:6]

Write a program to demonstrate the use of “del” statement to remove elements of a given List.

num [11, 22, 33, 44, 55, 66, 77, 88, 99)
print(“\nCurrent List = “, num)
k=int(input(“\nEnter the position of element to delete:”))
del num[k]
print(“\nList after deletion = “, num)
del num[2]
print(“\nList after deleting first two elements:”, num)

Copying the List

1) By Assignment Method- This is assignment method and it actually does not creates another copy of the list instead it gives another name to the same list. So, in the above example the list which was named ‘num’, after the assignment statement it will have another name as ‘num2.

<second list name> <first list name>

2) copy () function-


<second list name>=<first list name>.copy()

Program 45 Write a program to demonstrate the ways in which copies of the list can

A=[11, 22, 33, 44, 55]
print(“\nFirst List A=”,A)
print(“Copied List B=”, B)
print(“\nList B=”, B)
print(“List A=”,A)
print(“List C=”,C)
print(“List C=”,C)
print(“List A”,A)

Functions and Methods of List

Every List that we create in Python is actually an object of List class, and being class, List of many built-in functions and method for List manipulation.

1) len()- This function is used to calculate and return length of the given List. It calculates length in t of number of characters.

len(<list name>)



2) append()- This is a List class method which is used to add a new element at the end of List.


<list name>.append(<new element>)



3) extend()- This is a List method, which is used to add one list into another. The list to be added gets added at the end of the first List.





4) insert()- This List method is also used to add one element in the list like append() and extend().However both extend() and append() inserts element at the end of List whereas insert() can be used t add element at any given position in the List.

<list name>.insert(<position>, <element>)


The above example first creates a List ‘num’ with 5 integers. Next, it inserts a new element (33 at the given position (2) within the List. Finally it prints the List, which appears as~


[11, 22, 33, 44, 55, 66]
num=[11,22,44,55,66] n

In the backward indexing of List, index positions are negative values, so, we can use negative values also for position of insertion. The above example will add the new element (33) at the 3″ position in the list from the back. After insertion the List will be printed as

[11, 22, 33, 44, 55, 66]


num.insert(-15, ‘aa’)

This example will add new element (‘aa’) at the beginning of the List. Any position which is less then equal to -7 will add element at the beginning of this list.


num.insert(25, ‘zz’)

This example will add the new element (‘zz’) at the end of List. Any position which is more then equal to 6 will add element at the end of the list.

5) pop()- This method is used to remove element from the list. In this method we specify the position of the element to remove. If the position or index is not specified in the method it will remove the last element from the list.

<list name>.pop(<index>)


lst=[‘e’, ‘t’, ‘f’, ‘h’, ‘a’, ‘o’]

The above example will first create a List of letters by the name ‘lst’. Next, it will delete element (‘h’) at the 3rd position from the list. Finally, it will print the updated list, which is –
[‘e’, ‘t’, ‘f’, ‘a’, ‘o’]



This example will delete second last element (‘a’) from the list and then print the updated list as-

[‘e’, ‘t’, ‘f’, ‘o’]



This example will delete the last element (‘o’) from the list because we have not specifie the index of the element and then print the updated list as

For Complete Live Class click on the link below

Thansk For Reading the block i hope you will understand the topic well.

Also Check

list in python with examples- Python List

python for loop syntax – Looping Statement

Leave a Comment