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’]
C=A+B
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.
num=[11,22,33]
print(num*3)
newnum=num*4
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
syntax

<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]

print(num[1:7:2]#

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:])
num2=num[:6]
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.

Syntax

<list name>.append(<element>)

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

num=[]
for a in range(10):
k=int(input(“Enter a number”))
num.append(k)
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

Syntax

<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: “))
name[pos]=k
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.

Syntax-1

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.

Syntax-2

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.
Syntax-1

<second list name> <first list name>

2) copy () function-

Syntax-2

<second list name>=<first list name>.copy()
num2=num.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)
B[2]=34
B=A
print(“Copied List B=”, B)
print(“\nList B=”, B)
print(“List A=”,A)
C=A.copy()(
print(“List C=”,C)
C[0]=12
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.

Syntax
len(<list name>)

Example

A=(11,22,33,44,55]
print(len(A))

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

Syntax

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

Example

A=[11,22,33]
A.append(44)

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.

Syntax

<list1>.extend(<list2>)

Example

A=(11,22,33)
B=[44,55]
A.extend(B)
print(A)

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.

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

num=[11,22,44,55,66]
num.insert(2,33)
print(num)

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~

Example-2

[11, 22, 33, 44, 55, 66]
num=[11,22,44,55,66] n
num.insert(-3,33)
print(num)




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]

Example-3

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.

Example-4

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.

Syntax-
——-
<list name>.pop(<index>)

Example-1

lst=[‘e’, ‘t’, ‘f’, ‘h’, ‘a’, ‘o’]
lst.pop(3)
print(lst)

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’]

Example-2

lst.pop(-2)
print(1st)

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

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

Example-3

lst.pop()
print(lst)

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
[‘e’,’t’,’f’]

For Complete Live Class click on the link below

https://youtu.be/RaUQV725Ghw

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