Python Lists: A Comprehensive Guide
Python Lists: A Comprehensive Guide
List in Python
Understanding the importance of the list
1
2025-09-19
This content covers the topics from these books and more..
2
2025-09-19
YouTube Website
© 2025 Rohit Singh. All rights reserved.
3
2025-09-19
What is List
Example
List1=[10,50.00,'Amit',100.00]
print(List1)
[10, 50.0, 'Amit', 100.0]
4
2025-09-19
❑ eval() is the method used to get any data type from the user.
❑ list() is the type conversion where we can convert any datatype to a list.
Example
Example
st1=list(input('Enter value: '))
st=eval(input('Enter list: '))
print(st1)
print(st)
#output
#output
Enter value: python
Enter List: [10,20,30]
['p', 'y', 't', 'h', 'o', 'n']
[10, 20, 30]
Don’t forget the appropriate bracket.
© 2025 Rohit Singh. All rights reserved.
5
2025-09-19
Accessing Elements
We have to use an index that can access an element of a list using positive or negative values. Slicing can
also be done using an index in the form of [start, stop, step] to access the elements.
Indexing
print(L[5]) print(L[3]) print(L[-9]) print(L[-2])
print(L[9-8]) print(L[5*0]) print(L[50]) print(L[len(L)-4])
6
2025-09-19
Go Slow, doubling the speed will not double the speed of understanding [Link]
7
2025-09-19
Accessing Elements
m=[10,20,30,[40,50,60],70,80,90] n=[[10,20],[30,40],[50,60]]
sample = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
1) Reverse the list
2) Get [90, 70, 50, 30]
3) Get the 4th, 3rd, and 2nd
4) return every third element.
5) Get the first 3 elements in reverse order.
6) Exclude the first and last elements
8
2025-09-19
Traversing a List
It is a way of visiting each element of the list individually at a time. It can be done using a for and while loop.
Example Example
m=[10,15,20,25,30] m=[13,15,90,12,15]
for i in m: for i in range(0,len(m)):
print(i) print(list[i],type(m[i]))
© 2025 All
© Copyright Rohit
TheSingh.
RightsAll
Arerights reserved.
Reserved
Write a program to input a list of elements and print only the positive values.
9
2025-09-19
Write a program to print only even elements from the list: L = [10, 13, 12, 11, 15, 18]
Write a program to accept a list from the user and display the numbers that are divisible by 7.
10
2025-09-19
Write a program that accepts a list from the user and displays the numbers that end with 3.
Write a program to count the occurrences of even and odd numbers from a list entered by the user.
11
2025-09-19
Write a program to display the smallest and largest values of the list entered by the user.
Write a program that prints the table of the first even number.
like the list is L = [23, 13, 101, 6, 81, 4], so the table of 6 will be printed.
© 2025 Rohit Singh. All rights reserved.
12
2025-09-19
YouTube Website
© 2025 Rohit Singh. All rights reserved.
List1=list(range(10,15)) List1=list(range(10))
print(List1) print(List1)
[10,11,12,13,14] [0, 1, 2, 3, 4,5,6,7,8,9]
13
2025-09-19
Updating a List
A list is a mutable data type, i.e. you can change the elements of a list in place. we can update single or
multiple elements of lists by slicing or indexing.
Example Example
m=[10,20,30,40,50] li=[10,15,[100,200],20,25]
m[2]=400 li[2]=400
print(m) print(li)
[10, 20, 400, 40, 50] [10, 15, 400, 20, 25]
Write a program to increase the even number twice and triple the odd number from a list
14
2025-09-19
Write a program that converts all the odd numbers in the list to even by adding 1.
Write a program to count the frequency of a given number by the user in a list. If a number is not in the list,
it should print number is not available.
15
2025-09-19
Comparison of List
Membership Operator
The membership operator is used to check whether a specific element exists in a list or any other sequence.
It helps in validating the presence or absence of a value. If the element is found in the sequence, the
expression returns True, otherwise, it returns False.
16
2025-09-19
Repetition/Replication
The repetition (or replication) operator is used to repeat the elements of a sequence, such as a string or a
list, multiple times. It is represented by the symbol (*). When a sequence is multiplied by a number using
this operator, the entire sequence is repeated that many times in the result.
Concatenation
It is used to join two sequences into a single sequence. It is represented by the symbol (+). When applied, it
combines the elements of both sequences in the same order without altering them.
Go Slow, doubling the speed will not double the speed of understanding [Link]
17
2025-09-19
INDEX
index operator is used to access individual elements of a sequence. Each element in the sequence is
assigned a position number called an index. By using the index, we can directly retrieve the value stored at
that position.
Example Example
li=[10,20,50,90,2] li=[10,20,50,90,2]
print([Link](20)) print([Link](30))
1 ValueError
18
2025-09-19
append
append() is used to add a single element to the end of a list. It increases the size of the list by one and places the new
item as the last element. Syntax: [Link](item), where item is the value you want to add.
© 2025 All
© Copyright Rohit
TheSingh.
RightsAll
Arerights reserved.
Reserved
Write a program to create n lists after getting each value from the user one by one
19
2025-09-19
Extend()
extend() method is used to add multiple elements to the end of a list at once. It takes an iterable (like
another list, tuple, or string) and adds each of its elements individually to the list. This method is useful
when you want to combine or merge sequences into a single list. Syntax: [Link](iterable)
Example
L1 = [1, 2, 3, 4]
A = [100, 90, 80, 50]
[Link](A)
print(L1)
[1, 2, 3, 4, 100, 90, 80, 50]
© 2025 Rohit Singh. All rights reserved.
YouTube Website
© 2025 Rohit Singh. All rights reserved.
20
2025-09-19
Insert()
insert() method is used to add a new element at a specific position in a list. It takes two arguments: the
index (position) where the element should be inserted, and the item to be inserted.
Example
L5 = [10,20,30]
[Link](2, 15)
print(L5)
[10,20,15,30]
del
del statement is used to delete elements, slices, or even the entire list. del does not return the deleted
value; it simply removes it. Syntax: del list[index] to delete an element at a specific position, or del
list[start:end] to delete a range of elements.
Example Example
L1 = [10, 20, 30, 40, 50] L1 = [10, 20, 30, 40, 50]
21
2025-09-19
pop()
pop() method is used to remove and return an element from a list. By default, it removes the last element,
but you can also specify the index of the element you want to remove.
Syntax: [Link]([index]), where index is optional.
Example Example
L1 = [10, 20, 40,50,60] L1 = [10, 20, 40,50,60]
[Link]() [Link](1)
print(L1) print(L1)
[10, 20, 40, 50] [10, 40, 50, 60]
remove()
remove() method is used to delete the first occurrence of a specific element from a list. Unlike pop(), which
works with indexes, remove() works with the actual value of the element.
Syntax: [Link](item), where item is the value to be removed.
Example Example
L1 = [10,50,30,40] L1 = [10,20,30,40,50]
[Link](50) [Link](30)
print(L1) print(L1)
[10,30,40] L1 = [10,20,40,50]
22
2025-09-19
EXAMPLE
EXAMPLE: Write the most appropriate list method to perform the following task in a given list myList:
23
2025-09-19
sort()
sort() method is used to arrange the elements of a list in a specific order. By default, it sorts the list in
ascending order, but you can also sort in descending order by using the parameter reverse=True.
Syntax: [Link]() or [Link](reverse=True)
Example Example
l1=[20,2,55,7,900,56] l1=[ 'A', 'D', 'Z', 'B']
[Link]() [Link](reverse=True)
print(l1) print(l1)
[2, 7, 20, 55, 56, 900] ['Z', 'D', 'B','A']
© 2025 Rohit Singh. All rights reserved.
Go Slow, doubling the speed will not double the speed of understanding [Link]
reverse
reverse() method is used to reverse the order of elements in a list. It does not sort the list but simply flips
the existing order, making the last element come first and the first element come last.
Syntax: [Link]()
Example
li=[10,20,50,90,2]
[Link]( )
print(li)
[2, 90, 50, 20, 10]
© 2025 Rohit Singh. All rights reserved.
24
2025-09-19
L = [ 10,20,30,40,50]
a) To print the list L in descending order
b) To add a new number 67 in the list L.
c) To add a new list L2=[34,56] to the above list L.
d) To print the sum of all the elements of the list L.
e) To repeat the elements of the list 5 times.
Copy
copy() method is used to create a shallow copy of a list. This means it makes a new list with the same
elements as the original, but both lists are stored at different memory locations.
Syntax: new_list = [Link]()
Example Example
List=[10,15,20,25,30] myList = [10, 20, 30]
Lst=[Link]( ) print("Original list:", myList)
print(Lst)
myList[1] = 99
[10,15,20,25,30]
print("Modified list:", myList)
25
2025-09-19
clear()
clear() method is used to remove all the elements from a list, making it empty. It does not delete the list
itself but only clears its contents. Syntax: [Link]()
Example
l1=[20,2,55,7,900,56]
[Link]( )
print(l1)
[ ]
Count
count() method is used to find the number of times a specific element appears in a list. It scans the list and
returns an integer value representing the frequency of that element.
Syntax: [Link](item), where item is the value you want to count.
Example Example
l1=[20,2,55,55,55,7,900,56] l1=[20,2,55,55,[55,10],7,900,56]
n=[Link](55)
n=[Link](55)
print(n)
print(n)
3 2
26
2025-09-19
length
len() function is used to find the total number of elements in a list (or any sequence like string, tuple, etc.).
It returns an integer value representing the length of the sequence. Syntax : len(list)
Example Example
l1=[20,2,55,55,55,7,900,56] l1=[20,2,[55,55,55],7,900,56]
n=len(l1) n=len(l1)
print(n) print(n)
8 6
l1=[20,27,900,56]
print(len(l1))
4
27
2025-09-19
EXAMPLE
Start=[8,9,10]
(a) Set the second entry (index 1) to 17.
(b) Add 4,5 and 6 to the end of the list.
(c) Remove the first entry from the list.
(d) Sort the list
(e) Print the list element twice.
(f) Insert 25 at index 3
Write a code to display all the names starting from the alphabet 'p' after getting the list.
like the list is L = [23, 13, 101, 6, 81, 4], so the table of 6 will be printed.
© 2025 Rohit Singh. All rights reserved.
28
2025-09-19
Write a program to find the index of the given number from a List using linear search
like the list is L = [23, 13, 101, 6, 81, 4], so the table of 6 will be printed.
© 2025 Rohit Singh. All rights reserved.
Accepts a list and finds the sum of all even numbers and the sum of all odd numbers.
like the list is L = [23, 13, 101, 6, 81, 4], so the table of 6 will be printed.
© 2025 Rohit Singh. All rights reserved.
29
2025-09-19
The record of a student is stored in the list name strecord=['Aman',1,[80,70,80,90,100],89.00] where values
represent the [name, roll no, marks in five subjects and percentage].
a Sort the marks of the student.
b Remove the first element of the list.
c Delete the 3rd element from the list.
d Display the marks of the fifth subject.
e Display the Percentage of the student.
f To arrange the marks in descending order.
g Change the name of a student to 'Raghav'.
h Insert an element 25 at the 3rd index in the list.
© 2025 Rohit Singh. All rights reserved.
The record of a student is stored in the list name strecord=['Aman',1,[80,70,80,90,100],89.00] where values
represent the [name, roll no, marks in five subjects and percentage].
i Double the value of the list.
j Set the value of roll no to 17
k Display the maximum marks.
l Display the Roll no. of the student.
m Add any element at the end of the list.
n Add 40,15 and 60 to the end of the list
o Find whether the given element is present or not.
p Delete a given element by the user from the list.
© 2025 Rohit Singh. All rights reserved.
30
2025-09-19
SUBSCRIBE NOW
AND BE A PART OF OUR TEAM
Thank You !
31