List Manipulation in Python Class 11 Notes, Lists are ordered collections of datatype, which can store multiple items. This chapter covers how to create lists, slicing and indexing, etc. This note helps the students to become masters in Python.
List Manipulation in Python Class 11 Notes
Introduction to List
In Python, Multiple values, number, character, date etc. can be stored in a single variable by using lists, a list is an ordered sequence of elements that can be changed or modified. Lists are defined by having values inside square brackets [].
List of integers, Example:
list1 = [2, 4, 6, 8, 10, 12]
print(list1)
Output:
[2, 4, 6, 8, 10, 12]
List of characters, Example:
list2 = ['a', 'e', 'i', 'o', 'u']
print(list2)
Output:
['a', 'e', 'i', 'o', 'u']
List with mixed data types, Example:
list3 = [100, 23.5, 'Hello']
print(list3)
Output:
[100, 23.5, 'Hello']
Empty List, Example:
list4 = []
print(list4)
Output:
[]
Accessing Elements in a List (Display Element from the List)
The elements of a list are accessed in the same way as characters are accessed in a string.
Example –
list1 = [2, 4, 6, 8, 10, 12]
print(list1[0])
print(list1[3])
print(list1[-1])
print(list1[-3])
print(list1[15])
Output:
2
8
12
8
IndexError: list index out of range
Lists are Mutable
In Python, lists are mutable. It means that the contents of the list can be changed after it has been created.
Example –
list1 = ['Red', 'Green', 'Blue', 'Orange']
list1[3] = 'Black'
print(list1)
Output:
['Red', 'Green', 'Blue', 'Black']
List Manipulation in Python Class 11 Notes
List Operations
The data type list allows manipulation of its contents through various operations as shown below.
Concatenation
Python allows us to join two or more lists using concatenation operator depicted by the symbol +.
Example 1 –
list1 = [1, 3, 5, 7, 9]
list2 = [2, 4, 6, 8, 10]
result = list1 + list2
print(result)
Output:
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Example 2 –
list3 = ['Red', 'Green', 'Blue']
list4 = ['Cyan', 'Magenta', 'Yellow', 'Black']
result = list3 + list4
print(result)
Output:
['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
Repetition
Python allows us to replicate a list using repetition operator depicted by symbol *.
list1 = ['Hello']
print(list1 * 4)
Output:
['Hello', 'Hello', 'Hello', 'Hello']
Membership
Like strings, the membership operators in checks if the element is present in the list and returns True, else returns False.
list1 = ['Red', 'Green', 'Blue']
print('Green' in list1)
print('Cyan' in list1)
Output:
True
False
Slicing
Like strings, the slicing operation can also be applied to lists.
Example 1 –
list1 = ['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
print(list1[2:6])
Output:
['Blue', 'Cyan', 'Magenta', 'Yellow']
Example 2 –
list1 = ['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
print(list1[2:20])
Output:
['Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
List Manipulation in Python Class 11 Notes
Traversing a List
We can access each element of the list or traverse a list using a for loop or a while loop.
List Traversal Using for Loop –
Example –
list1 = ['Red', 'Green', 'Blue', 'Yellow', 'Black']
for item in list1:
print(item)
Output:
Red
Green
Blue
Yellow
Black
List Traversal Using while Loop –
Example –
list1 = ['Red', 'Green', 'Blue', 'Yellow', 'Black']
i = 0
while i < len(list1):
print(list1[i])
i += 1
Output:
Red
Green
Blue
Yellow
Black
List Methods and Built-in Functions
The data type list has several built-in methods that are useful in programming.
len()
Returns the length of the list passed as the argument
list1 = [10, 20, 30, 40, 50]
length = len(list1)
print("The length of the list is:", length)
Output:
The length of the list is: 5
list()
Creates an empty list if no argument is passed Creates a list if a sequence is passed as an argument
Example –
list1 = list()
print("Empty list:", list1)
Output:
Empty list: []
append()
Appends a single element passed as an argument at the end of the list. The single element can also be a list.
Example –
list1 = [10, 20, 30, 40]
list1.extend([50, 60])
print(list1)
Output:
[10, 20, 30, 40, 50, 60]
extend()
Appends each element of the list passed as argument to the end of the given list.
Example –
list1 = [10, 20, 30]
list2 = [40, 50]
list1.extend(list2)
print("Merged list:", list1)
Output:
Merged list: [10, 20, 30, 40, 50]
insert()
Inserts an element at a particular index in the list
Example –
list1 = [10, 20, 30, 40, 50]
print("Original list:", list1)
list1.insert(2, 25)
print("After inserting 25 at index 2:", list1)
Output:
Original list: [10, 20, 30, 40, 50]
After inserting 25 at index 2: [10, 20, 25, 30, 40, 50]
count()
Returns the number of times a given element appears in the list
Example –
list1 = [10, 20, 30, 10, 40, 10]
num_to_count = 10
count_result = list1.count(num_to_count)
print(count_result)
Output:
3
index()
Returns index of the first occurrence of the element in the list. If the element is not present, ValueError is generated.
Example –
list1 = [10,20,30,20,40,10]
print(list1.index(20))
Output:
1
remove()
Removes the given element from the list. If the element is present multiple times, only the first occurrence is
removed. If the element is not present, then ValueError is generated.
Example –
list1 = [10,20,30,40,50,30]
list1.remove(30)
print(list1)
Output:
[10, 20, 40, 50, 30]
pop()
Returns the element whose index is passed as parameter to this function and also removes it from the list. If no parameter is given, then it returns and removes the last element of the list.
Example –
list1 = [10, 20, 30, 40, 50, 60]
removed_item = list1.pop(3)
print(list1)
Output:
[10, 20, 30, 50, 60]
reverse()
Reverses the order of elements in the given list
Example –
list1 = [34, 66, 12, 89, 28, 99]
list1.reverse()
print("Reversed list:", list1)
Output:
Reversed list: [99, 28, 89, 12, 66, 34]
sort()
Sorts the elements of the given list in-place
Example –
list1 = ['Tiger', 'Zebra', 'Lion', 'Cat', 'Elephant', 'Dog']
list1.sort()
print("Sorted list:", list1)
Output:
Sorted list: ['Cat', 'Dog', 'Elephant', 'Lion', 'Tiger', 'Zebra']
sorted()
It takes a list as parameter and creates a new list consisting of the same elements arranged in sorted order.
Example –
list1 = [23, 45, 11, 67, 85, 56]
list2 = sorted(list1)
print("Sorted list:", list2)
Output:
Sorted list: [11, 23, 45, 56, 67, 85]
min()
Returns minimum or smallest element of the list
Example –
list1 = [34, 12, 63, 39, 92, 44]
min_value = min(list1)
print("The smallest number in the list is:", min_value)
Output:
The smallest number in the list is: 12
max()
Returns maximum or largest element of the list
Example –
list1 = [34, 12, 63, 39, 92, 44]
max_value = max(list1)
print("The largest number in the list is:", max_value)
Output:
The largest number in the list is: 92
sum()
Returns sum of the elements of the list
Example –
list1 = [34, 12, 63, 39, 92, 44]
total_sum = sum(list1)
print("The sum of the list elements is:", total_sum)
Output:
The sum of the list elements is: 284
Nested Lists
When a list appears as an element of another list, it is called a nested list.
Example –
list1 = [1, 2, 'a', 'c', [6, 7, 8], 4, 9]
element = list1[4]
print("Element at index 4:", element)
Output:
Element at index 4: [6, 7, 8]
Copying Lists
The simplest way to make a copy of the list is to assign it to another list.
Example –
list1 = [1, 2, 3]
list2 = list1
print("list2:", list2)
Output:
list2: [1, 2, 3]
Computer Science Class 11 Notes important links
- Computer Systems and Organisation
- Introduction to problem solving
- Getting Started with Python
- Flow of Control statements in Python
- String in Python
- Lists in Python
- Tuples in Python
- Dictionary in Python
- Society, Law and Ethics
Disclaimer: We have taken an effort to provide you with the accurate handout of “List Manipulation in Python Class 11 Notes“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 11 CBSE Textbook, Sample Paper, Old Sample Paper, Board Paper, NCERT Textbook and Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in