Python Unit - II Lists (1)
Python Unit - II Lists (1)
LIST MANIPULATION
➢ List is a container that is used to store a list of values with any (different)
datatypes.
➢ List Values are represented in square brackets [] and separated by comma.
➢ The List elements can be modifiable/ mutable datatypes.
(e.g.) L=[1,2,3,4,5]
M=[1.2, 3.4, 5.6]
n=[‘a’,’z’,’msd’,’india’]
p=[‘india’,’I’,7.8,55]
#Empty List
a=[]
# Long List
b=[2,3,4,5,6,7,8,9,1,11,22,33,44,55,66,77,88,99,100,
101,102, 103,104,105,106,107,108,109,110]
L=[11,21,31,41,51]
Index Values: L[0]=11, L[1]=21, L[2]=31, L[3]=41, L[4]=51
*Computational Thinking and Programming - I
Nested Lists:
A List can have an element in it which itself is a list, such a list is called
nested list.
(e.g.)
*Computational Thinking and Programming - I
Creating Lists from Existing Sequence:
➢ We can create lists from sequences using built in list type object.
➢ L=List(<Sequence>)
➢ ‘<Sequence>’can be any kind of sequence objects including string,
tuples & lists.
(e.g.)
*Computational Thinking and Programming - I
L=[1,’hai’,3.2]
Comparing Lists:
➢ We can compare two lists using standard comparison operators
(<,>,<=,>=,==,!=,etc.).
➢ Python compare individual elements of lists in lexicographical order.
(e.g.) l1=[1,2] l2[3,4]
l1[0] vs l2[0]
l1[1] vs l2[1]
*Computational Thinking and Programming - I
*Computational Thinking and Programming - I
List Operations:
➢ Joining(Concatenation)
➢ Replicating(Repetition)
➢ Slicing
Joining(Concatenation)
To join two (or) more than lists ‘ + ’ operator is used.
(e.g.)
*Computational Thinking and Programming - I
Replicating(Repetition)
Like String we can use ‘ * ’ Operator to replicate a list specified number
of times.
(e.g.)
Slicing
Like Strings Slices, List Slices are the subpart of a list extracted out. We
can ‘Indexes’ of the list elements to create ‘list slices’.
(e.g.)
seq=L[ Start: Stop: Step ]
List slice namely ‘seq’ having of list ‘L’ on indexes
“Start,Start+1,Start+2,………Stop-1,Step (Increment)”. Recall that index on last
limit is not included in the ‘Last Slice’
*Computational Thinking and Programming - I
*Computational Thinking and Programming - I
List Functions and Methods
Python has many built in (in built) list methods used for list manipulation.
Syntax: <listobject>.<method name>()
The ‘ len() ’Method:
This method used to return length(number of elements) of the list.
(e.g.)
The ‘ index() ’ Method:
This function is used returns the index of “First Matched” element from
the list.
(e.g.)
*Computational Thinking and Programming - I
The ‘ append() ’ Method:
This method used to add an elements to the end of the list, using this
method only we can add single element at a time.
(e.g.)
*Computational Thinking and Programming - I
The ‘ extend() ’ Method:
This method used to add ‘N’ elements to the end of the list, using this
method only we can add only multiple elements at a time.
(e.g.)
*Computational Thinking and Programming - I
The ‘ insert() ’ Method:
(a)This method also used to add an elements in the existing the list like
append() and extend().
But Insert() method is used to add element at “any position” of our
choice in the list.
(e.g.)
*Computational Thinking and Programming - I
(b) ‘Update’
This is to update (or) change an element of the list in place, we can
assign a new value to the element’s index in list.
(e.g.)
*Computational Thinking and Programming - I
The ‘ pop() ’ Method:
(a) This method is used to remove the element from the list using index.
(e.g.)
(b) This ‘del’ statement used to remove an individual or all the elements
identified by slice.
(e.g.)
*Computational Thinking and Programming - I
The ‘ remove() ’ Method:
This method removes the first occurrence of given ‘ element ’ from the list.
(e.g.)
The ‘median() ’ Method: This method used to return the middle value of the list.
*Computational Thinking and Programming - I
lst=eval(input("Enter List:"))
length=len(lst)
mean=sum=0
for i in range(0,length):
sum+=lst[i]
mean=sum/length
print("Given List is :",lst)
print("The Mean of the Given List is:",mean)
Output:
Linear Search
➢ Linear Search is search algorithm which is used to search a element in an List.
➢ X= Search Element
➢ Compare X with each elements of the List, if its matches found then it return the
position of the element.
(e.g.)
lst=eval(input("Enter the List:"))
length=len(lst)
element=int(input("Enter the Element to be Searched for:"))
for i in range(0,length):
if element==lst[i]:
print(element,"is Found at Index : ",i)
break
else:
print(element,"is Not Found in the Given List")
Output:
*Computational Thinking and Programming - I
Program to Count the Frequency of a given element in a List of Numbers
(e.g.)
lst=eval(input("Enter the List:"))
length=len(lst)
element=int(input("Enter the Element to Count the Frequency:"))
count=0
for i in range(0,length):
if element==lst[i]:
count+=1
if count==0:
print(element,"is Not Found in the Given List")
else:
print(element," Has Frequency as",count,"in the Given List")
Output:
Binary Search
➢ Binary search is search algorithm which is used to search a element in
ordered array.
➢ X= Search Element
➢ Compare X with the Middle Element.
➢ If X matches with Middle Element, we return the Mid index.
➢ Else if X is greater than the Middle Element, then X can only lie in
right half sub array after the mid element.so we recur for right half.
➢ Else X is smaller than the Middle Element, then X can only lie in left
half sub array.so we recur for left half.
Eg: a=[3,4,8,9,10,12,16,17,18]
X=10 # X Present in Middle
X=18 # X Present in Right to the Middle Value 10
X=4 # X Present in Left to the Middle Value 10
Binary Search
def binary_search(ar,key):
low=0
high=len(ar)-1 Output:
mid=0
while low<=high:
mid=int((high+low)/2)
if ar[mid]<key:
low=mid+1
elif ar[mid]>key:
high=mid-1 #6
else:
return mid # 5
else:
return -999
ar =[7, 10, 17, 18, 19, 21, 32, 44, 45, 72]
S=int(input("Enter the Element to be Searched for:"))
result = 5
if result>=0:
print(S," is Found at Index:", result)
else:
print("Sorry !",S," is Not Found in the List")
*Computational Thinking and Programming - I
THANK YOU