0% found this document useful (0 votes)
233 views46 pages

CH 11 List Manipulation

Here are the steps to sort the given lists using bubble sort and insertion sort: Bubble Sort: 1) [89,20,31,56,20] Pass 1: Compare adjacent elements and swap if not in order - 89 & 20, 20 & 31, 31 & 56, 56 & 20. No swaps. Pass 2: Compare adjacent elements and swap if not in order - 20 & 31, 31 & 56, 56 & 20, 20 & 89. Swap 20 & 89. Pass 3: Compare adjacent elements and swap if not in order - 31 & 56, 56 & 20, 20 & 31, 89 & 31. No swaps. Sorted list: [20, 31, 56

Uploaded by

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

CH 11 List Manipulation

Here are the steps to sort the given lists using bubble sort and insertion sort: Bubble Sort: 1) [89,20,31,56,20] Pass 1: Compare adjacent elements and swap if not in order - 89 & 20, 20 & 31, 31 & 56, 56 & 20. No swaps. Pass 2: Compare adjacent elements and swap if not in order - 20 & 31, 31 & 56, 56 & 20, 20 & 89. Swap 20 & 89. Pass 3: Compare adjacent elements and swap if not in order - 31 & 56, 56 & 20, 20 & 31, 89 & 31. No swaps. Sorted list: [20, 31, 56

Uploaded by

ROCK RACK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

COMPUTER SCIENCE-

XI
BY
RAJA MAHESHWARI
CHAPTER – 11
LIST
MANIPULATION
LIST
• A list in Python represents a list of comma-
separated values of any datatype between
square brackets.
• It is mutable.
• Eg: [10,20,30,40,50]
[‘a’, ‘e’, ‘I’, ‘o’, ‘u’]
[1004, ‘Swetha’, 86,95,46,85,49]
Note: Lists and Dictionary are mutable.
Strings and Tuple are immutable.
CREATING LIST
• To create list, put the items separated by
commas and enclosed with square brackets [ ].
• Empty lists: [ ]
• Long lists: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
• Nested lists: A list can have an element in it,
which itself is a list. Eg: [1,6,[5,8],15]
Creating LIST
• A list can be created from sequences as per
syntax: L= list(<sequence>)
Example Output
L=list(‘hello’) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
print(L)
T=(3,5,7,8) [3,5,7,8]
L1=list(t)
print(L1)
L2 = list(input(‘enter a list ’)) enter a list 1234
print(L2) [1,2,3,4]
L3 = eval(input(‘enter a list ’)) enter a list [1,2,3,4]
print(L3) [1,2,3,4]
eval() function
• The eval() function can be used to evaluate
and return the result of an expression given
as string.
Example Output
eval(‘5+4’) 9

eval(‘9*3’) 27

L = eval(input(‘enter a list ’)) enter a list [1,2,3,4]


print(L3) [1,2,3,4]
t = eval(input(‘enter a tuple ’)) enter a list (1,2,3,4)
print(t) (1,2,3,4)
Accessing Lists
• Elements of list can be accessed using index
same as strings i.e., forward indexing as
0,1,2,3…. and backward indexing as -1,-2,-3,…

• The list name stores reference(memory


location) of where its elements are stored
index wise.
find the output
x=[50,46,82, 39,16] Answers:
• len(x)
• x[3]
• x[-2]
• x[1:4]
• x[-2:-4]
• 76 in x
• [1,2]+[3,4]
• [1,2]*2
Traversing a LIST
• Traversal of a sequence means accessing and
processing each element of it.
• The for loop makes it easy to traverse or loop
over the times in a list.
comparing LISTs
• Two lists can be compared using comparison
operators like <,>,<=,>=,==,!=
• Eg:
List operations
• Joining Lists:
⮚Concatenation operator + is used to join two lists.
⮚Eg: [1,2,3] + [5,6,7] results in [1,2,3,5,6,7]
⮚[1,2,3] +4 gives error
⮚[1,2,3] + ‘abc’ gives error
List operations

• Repeating or Replicating Lists:


⮚To replicate a list for specified number of times use
* operator.
⮚Eg: [1,2]*3 result in [1,2,1,2,1,2]
List operations
• Slicing the Lists:
⮚To create list slices as per following format:
seq = L [start : stop : step]
It creates a list slice out of list L with elements falling
between indexes start and stop, not including stop
skipping step-1 elements in between.
L= [10,30,28,48,93,57,17,62,39 72]

Stateme Output
nt
L[3:-3]
L[3:30]
L[-13:10]
L[1:7:2]
L[::3]
L[4::2]
L[::-1]
Making copy of list
Using copy() function
• copy() function is used to create a
true copy of the list.
• Syntax is L.copy()
List function / method
1. len() : Returns the length of its argument list
i.e, number of elements in the passed list.
• Syntax : len(<list>)
2. list() : Returns a list created from the passed
argument which should be a sequence type.
• Syntax : list(<sequence>)
3. index() : Returns the index of first matched
item from the list
• Syntax : list.index(<item>)
4. append() : adds an item to the end of the
list.
• Syntax : list . append(<item>)
5. extend() : used for adding multiple elements
to a list.
• Syntax : list . extend(<list>)
6. insert() : inserts an element somewhere in
between or any position of your choice.
But append() and extend() insert the
element at the end of the list.
• Syntax : list . insert(<ind> , <item>)
7. pop() : remove the item from the list.
• Syntax : list . pop(<index>)
• Takes one optional argument and returns a
value – the item being deleted.
• If no index is specified, pop() removes and
returns the last item in the list.
8. remove() : remove the first occurrence of
given item from the list
• Syntax : list . remove(<value>)
• Takes one essential argument and does not
return anything.
Difference between pop()
and remove()
• pop() removes an individual item
and returns it, while remove()
searches for an item, and removes
the first matching item from the list
and it will not return any value.
9. clear() : removes all the items from the list
and the list becomes empty list after this
function. This function returns nothing.
• Syntax : list . clear()
10. count() : returns the count of the item
that you passed as argument.
• If the given item is not in the list, it returns
zero.
• Syntax : list . count()
11. reverse() : reverses the item of the list.
• It does not create any new list but replaces
the existing list.
• Does not return anything.
• Syntax : list . reverse()
12. sort() : sorts the items of the list, by
default in increasing order.
It does not create any new list but sorts the
existing list.
Syntax : list . sort([<reverse=False/True>])
12. sorted() : takes the name of the list as an
argument and returns a new sorted list with sorted
elements in it.
Syntax : sorted(<iterable_sequence>,
[reverse=True/False])
Where <iterable_sequence> is the list to be sorted
(it can be any iterable sequence like list, tuple etc)
Arguments reverse is optional. If reverse argument
is missing, then list elements are sorted in ascending
order(default).
If reverse is True, list will be displayed in descending
order.
12. min(), max(), sum(): It takes the sequence and
return the minimum, maximum and sum of the
elements of the list respectively.
Syntax: min(<list>), max(<list>), sum(<list>)
12. min(), max(), sum(): It takes the sequence and
return the minimum, maximum and sum of the
elements of the list respectively.
Syntax: min(<list>), max(<list>), sum(<list>)
NESTED List
A list within another list is nested list.
Eg: L=[3,[4,8],[5,13,[7,14],19]]
Find the value for:
a) L[1]
b) L[2]
c) L[1][1]
d) L[2][0]
e) L[2][2]
f) L[2][2][1]
TWO DIMENSIONAL
List
A two dimensional list is a list having all its elements as lists of
same shapes. It is also called as a list of lists.
Eg: L=[[4,5,6],[1,2,3]]
TYPES OF TWO DIMENSIONAL List
1. Regular 2D list : It is a nested lists with these properties:
• All elements of a 2D list have same shape (same dimendion
and length)
• The length of 2D list tells about Number of Rows in it.
( i.e., len(list))
• The length of single row gives the Number of Columns in it.
( i.e., len(list[n]))
2. Ragged list: A list that has lists with different shapes as its
elements is also a 2d list but it is an irregular 2d list, also
known as a ragged list.
Eg: L1 = [ [ 3, 5, 1], [8,6] ]
Creating a 2D list
1. Regular 2D list : It is a nested lists with these properties:
• All elements of a 2D list have same shape (same dimendion
and length)
• The length of 2D list tells about Number of Rows in it.
( i.e., len(list))
• The length of single row gives the Number of Columns in it.
( i.e., len(list[n]))
2. Ragged list: A list that has lists with different shapes as its
elements is also a 2d list but it is an irregular 2d list, also
known as a ragged list.
Eg: L1 = [ [ 3, 5, 1], [8,6] ]
sorting
The process of arranging the elements in a specific
order i.e., ascending order or descending order.

TYPES OF sorting
• Bubble Sort
• Insertion Sort
• Selection Sort
• Merge Sort
• Quick Sort
Bubble sort
It is to compare two adjoining values and exchange
them if they are not in proper order.
Program to sort a list using bubble sort

https://youtu.be/nmhjrI-aW5o
Calculating no. of operations
Insertion sort
• It builds a sorted list one element at a time from the
unsorted list.
• It virtually divides the list of elements in two sub
lists : sorted sub-list and unsorted sub-list.
• Initially the entire list is unsorted sub-list and the
sorted sub-list is empty. Then it takes one element
from the unsorted sub list and inserts it into the
correct position in the sorted sub-list.
• It is not efficient on large datasets but efficient for
small datasets.
Insertion sort

https://youtu.be/OGzPmgsI-pQ
Program to sort a list using bubble sort
Sort the following list using
bubble sort and insertion sort
• [89,20,31,56,20]
• [34,8,14,51,32,21]
• [32,33,5,2,14,-4,22,39,34,-9]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy