0% found this document useful (0 votes)
8 views22 pages

Python Lists: Operations & Functions

This document provides an overview of lists in Python, covering their definition, indexing, various operations (such as concatenation, repetition, and membership), and built-in functions for manipulating lists. It also explains how to traverse lists using loops and introduces nested lists. Key built-in functions discussed include len(), append(), extend(), insert(), remove(), pop(), sort(), and others for list management.

Uploaded by

chopraneerav6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

Python Lists: Operations & Functions

This document provides an overview of lists in Python, covering their definition, indexing, various operations (such as concatenation, repetition, and membership), and built-in functions for manipulating lists. It also explains how to traverse lists using loops and introduces nested lists. Key built-in functions discussed include len(), append(), extend(), insert(), remove(), pop(), sort(), and others for list management.

Uploaded by

chopraneerav6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit II

Computational Thinking and


Programming – 1
(PART – 10 [lists]

CLASS XI COMPUTER SCIENCE


CODE 083
Topics Covered
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Lists: Introduction,
• Indexing,
• list Operations (concatenation, repetition, membership &
slicing)
• Traversing a list using loops
• Built-in functions: len(), list(), append(), extend(),
insert(), count(), index(), remove(), pop(), reverse(),
sort(), sorted(), min(), max(), sum();
• Nested lists
Lists SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

INTRODUCTION
• The data type list is an ordered sequence which is mutable and made
up of one or more elements.
• Unlike a string which consists of only characters, a list can have
elements of different data types, such as integer, float, string, tuple or
even another list.
• Elements of a list are enclosed in square brackets and are separated by
comma.
EXAMPLE
• list1 = [2,4,6,8,10,12]
• list2 = ['a','e','i','o','u']
• list3 = [100,23.5,'Hello']
INDEXING
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Each individual elements in a lists can be accessed using a technique


called indexing.
• The index specifies the element to be accessed in the list and is written
in square brackets ([ ]).
• The index of the first element (from left) in the string is 0 and the last
character is n-1 where n is the length of the string.

EXAMPLE
list1 = [2,4,6,8,10,12]
list1[0] #return first element of list1
Output - 2
LIST OPERATIONS SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

Concatenation.
• Python allows us to join two or more lists using concatenation operator
depicted by the symbol +.
list1 = [1,3,5,7,9]
list2 = [2,4,6,8,10]
Print(list1 + list2)
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Repetition
• Python allows us to replicate a list using repetition operator depicted
by symbol *.
list1 = ['Hello']
list1 * 4
['Hello', 'Hello', 'Hello', 'Hello']
Membership operation SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

IN
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']
'Green' in list1
True

NOT IN
The not in operator returns True if the element is not present in the list, else it
returns False.
list1 = ['Red','Green','Blue']
'Cyan' not in list1
True
Slicing Operation SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Like strings, the slicing operation can also be applied to lists.

Example
#positive indexes
list1 =['Red','Green','Blue','Cyan','Magenta','Yellow','Black']
list1[2:6]
['Blue', 'Cyan', 'Magenta', 'Yellow']

#negative indexes
#elements at index -6,-5,-4,-3 are sliced
list1[-6:-2]
['Green','Blue','Cyan','Magenta']
Traversing operation SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• We can access each element of the list or traverse a list


• using a for loop or a while loop.
Example
(A) List Traversal Using for Loop:
list1 = ['Red','Green','Blue','Yellow','Black']
for item in list1:
print(item)

Another way of accessing the elements of the list is using range() and len() functions:
for i in range(len(list1)):
print(list1[i])
Traversing operation SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• We can access each element of the list or traverse a list


using a for loop or a while loop.
Example
(B) List Traversal Using for while loop :
list1 = ['Red','Green','Blue','Yellow','Black']
i=0
while i < len(list1):
print(list1[i])
i += 1
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• len() - Returns the length of the list passed as the argument


>>> list1 = [10,20,30,40,50]
>>> len(list1)
5
• list() -Creates an empty list if no argument is passed
>>> list1 = list()
>>> list1
[]
Creates a list if a sequence is passed as an argument
>>> str1 = 'aeiou'
>>> list1 = list(str1)
>>> list1
['a', 'e', 'i', 'o', 'u']
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• append() - Appends a single element passed as an argument at the end


of the list. The single element can also be a list

>>> list1 = [10,20,30,40]


>>> [Link](50)
>>> list1
[10, 20, 30, 40, 50]

>>> list1 = [10,20,30,40]


>>> [Link]([50,60])
>>> list1
[10, 20, 30, 40, [50, 60]]
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• extend() - Appends each element of the list passed as argument to the


end of the given list

>>> list1 = [10,20,30]


>>> list2 = [40,50]
>>> [Link](list2)
>>> list1

[10, 20, 30, 40, 50]


Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• insert() - Inserts an element at a particular index in the list

>>> list1 = [10,20,30,40,50]


>>> [Link](2,25)
>>> list1
[10, 20, 25, 30, 40, 50]

>>> [Link](0,5)
>>> list1
[5, 10, 20, 25, 30, 40, 50]
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• count() Returns the number of times a given element appears in the


list
>>> list1 = [10,20,30,10,40,10]
>>> [Link](10)
3
• index() Returns index of the first occurrence of the element in the list.
If the element is not present, ValueError is generated
>>> list1 = [10,20,30,20,40,10]
>>> [Link](20)
1
>>> [Link](90)
ValueError: 90 is not in list
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• 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

>>> list1 = [10,20,30,40,50,30]


>>> [Link](30)
>>> list1
[10, 20, 40, 50, 30]

>>> [Link](90)
ValueError:[Link](x):x not
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• 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
>>> list1 = [10,20,30,40,50,60]
>>> [Link](3)
40
>>> list1
[10, 20, 30, 50, 60]

>>> list1 = [10,20,30,40,50,60]


>>> [Link]()
60
>>> list1
[10, 20, 30, 40, 50]
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• sort() Sorts the elements of the given list in-place

>>>list1 = ['Tiger','Zebra','Lion','Cat', 'Elephant' ,'Dog']


>>> [Link]()
>>> list1
['Cat', 'Dog', 'Elephant', 'Lion','Tiger', 'Zebra']

>>> list1 = [34,66,12,89,28,99]


>>> [Link](reverse = True)
>>> list1
[99,89,66,34,28,12]
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• reverse() Reverses the order of elements in the given list

>>> list1 = [34,66,12,89,28,99]


>>> [Link]()
>>> list1
[ 99, 28, 89, 12, 66, 34]

>>> list1 = [ 'Tiger' ,'Zebra' ,'Lion' , 'Cat' ,'Elephant' ,'Dog']


>>> [Link]()
>>> list1
['Dog', 'Elephant', 'Cat', 'Lion', 'Zebra', 'Tiger']
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• sorted() It takes a list as parameter and creates a new list consisting of


the same elements arranged in sorted order

>>> list1 = [23,45,11,67,85,56]


>>> list2 = sorted(list1)
>>> list1
[23, 45, 11, 67, 85, 56]
>>> list2
[11, 23, 45, 56, 67, 85]
Built-in Functions SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• min() Returns minimum or smallest element of the list

• max() Returns maximum or largest element of the list

• sum() Returns sum of the elements of the list

>>> list1 = [34,12,63,39,92,44]


>>> min(list1)
12
>>> max(list1)
92
>>> sum(list1)
284
Nested List SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• When a list appears as an element of another list, it is called a nested list

>>> list1 = [1,2,'a','c',[6,7,8],4,9]


#fifth element of list is also a list
>>> list1[4]
[6, 7, 8]

• To access the element of the nested list of list1, we have to specify two indices list1[i][j].
The first index I will take us to the desired nested list and second index j will take us to
the desired element in that nested list.

>>> list1[4][1]
7
#index i gives the fifth element of list1 which is a list
#index j gives the second element in the #nested list
THANKS FOR WATCHING..
DON’T FORGET TO SUBSCRIBE AND
SHARE

You might also like