List Data Type
List Data Type
The Python Lists are containers that are used to store a list values of any
type. List Data types in Python are mutable data type. It is a collections of
items and each item has its own index value, like String Data type. Index
of first item is 0 and the last item is n-1. Here n is number of items in a list.
Indexing of list is done in two directions: Forward direction which is known
as Positive Indexing and Backward direction is known as Negative
Indexing.
Indexing of list :
Positive Index
Value
Negative Index
Creating a List :
Page 1 of 18
LIST DATA TYPE
Nested List creation:
A list containing another list item within the defined list is called Nested list.
Example :
Nested List
L1 = [3, 4, [ 5, 7,], 9, 12]
eval() : The eval() function in a Python can be used to evaluate and return
the result of an expression given as string.
Example:
>>> eval(‘5’ + ‘8’)
The above statement will give a result as 13, even though ‘5’ and ‘8’ are used
as string.
The eval() function can be used both for input and output purpose. To use
eval() function as input statement following syntax can be used:
Syntax:
< Variable>= eval(input( “ Message ”))
Example:
var= eval(input(“ Enter value = ”))
print(var, type(var))
In above code :
if the value is entered in int form then output will be int and its data
type will be also int.
if the value is entered in float form then output will be float and its
data type will be also float.
if the value is entered in list form then output will be list and its data
type will be also list.
if the value is entered in tuple form then output will be tuple and its
data type will be also tuple.
Page 2 of 18
LIST DATA TYPE
Output :
3
5
9
Slicing of A List:
Output :
Page 3 of 18
LIST DATA TYPE
['I', 'N', 'D']
['I', 'A']
['I', 'N', 'D', 'I', 'A']
We can update single or multiple elements of lists by giving the slice on the left-hand
side of the assignment operator.
e.g.
Output
('Value available at index 2 : ', 1997)
('New value available at index 2 : ', 2001)
('New value available at index 3 : ', 2002)
Add Item to A List
append() method is used to add an Item to a List.
e.g.
list=[1,2]
print('list before append', list)
list.append(3)
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3])
NOTE : extend() method can be used to add multiple item at a time in list.
Page 4 of 18
LIST DATA TYPE
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 ‘+’.
Add Two Lists
e.g.
list = [1,2]
list2 = [3,4]
list3 = list + list2
print(list3)
OUTPUT
[1,2,3,4]
e.g :
list1 = [1,3,5,7,9]
list2 = [2,4,6,8,10]
list3 = list1 + list2
print(list3)
output :
[1, 3, 5, 7, 9, 2-, 4, 6, 8, 10]
Repetition:
Python allows us to replicate a list using repetition operator depicted by symbol ‘*’.
e.g. :
list1 = ['Hello']
print (list1 * 4)
Output:
Page 5 of 18
LIST DATA TYPE
Membership :
Like strings, the membership operators in checks if the element is present in the list
and returns True, else returns False.
Output :
True
False
The not in operator returns True if the element is not present in the list, else it returns
False.
Output :
True
False
Slicing :
Output :
['Blue', 'Cyan', 'Magenta', 'Yellow']
Output :
[ ] #results in an empty list
Output :
['Red','Green','Blue','Cyan','Magenta']
Output:
['Red','Blue','Magenta']
#negative indexes
Output :
['Green','Blue','Cyan','Magenta']
Output:
Page 7 of 18
LIST DATA TYPE
['Red','Blue','Magenta','Black']
Output:
['Black','Yellow','Magenta','Cyan','Blue', 'Green','Red']
list=[1,2]
print('list before append', list)
list.append(3)
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3])
NOTE :-extend() method can be used to add multiple item at a time in list.
eg - list.extend([3,4])
OUTPUT
[1,2,3,4]
Page 8 of 18
LIST DATA TYPE
Delete Item From A List
e.g.
list=[1,2,3]
print ('list before delete', list)
del list [1]
print('list after delete', list)
Output
e.g.
dellist [0:2] # delete first two items
dellist # delete entire list
Page 9 of 18
LIST DATA TYPE
Important methods and functions of List :
Function Description
Page 10 of 18
LIST DATA TYPE
list_of_elements= [4, 2, 8, 9, 3, 7]
x = int(input("Enter number to search: "))
found = False
for iin range(len(list_of_elements)):
if(list_of_elements[i] == x):
found = True
print("%d found at %dthposition"%(x,i))
break
if(found == False):
print("%d is not in list"%x)
Sorting :
Page 11 of 18
LIST DATA TYPE
1. Bubble Sort
2. Insertion Sort
Bubble Sort:
Bubble sort is a sorting process in which it compares two adjoining values and exchange
them if they are not in proper order. Here in single Pass/Iteration at a time n-1 exchange
occurs and the process is repeated until the whole sequence is sorted. To sort unsorted
value, Bubble sort requires maximum (n-1) pass to sort the element, but many times it
sorts the value before (n-1) pass.
Example:
12 26 7 80 38 56
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
Page 12 of 18
LIST DATA TYPE
Pass- 1 Action
12 26 7 80 38 56 No Exchange
12 26 7 80 38 56 Exchange
12 7 26 80 38 56 No Exchange
12 7 26 80 38 56 Exchange
12 7 26 38 80 56 Exchange
12 7 26 38 56 80 Final Value after Pass-1
Pass- 2 Action
12 7 26 38 56 80 Exchange
7 12 26 38 56 80 No Exchange
Now it has been found that the given sequence is now sorted. So no need to process
further. And here sorted elements has been found in Pass-2 only. This is final
sorted elements.
Write an Algorithm to Sort the Linear List by using Bubble sort method
Step 1 : [ Start]
Step 2 : [ Loop ]
For I = L to U
Step 3 : [ Loop]
For J=l to [ (U-1)-I ]
Step 4 : [Check Condition ]
If Ar[J]>Ar[J+1] then
Step 5 : [ Compute ]
Temp = Ar[J]
Step 6 : [ Compute ]
Ar[J]= Ar[J + 1]
Step 7 : [ Compute ]
Ar[J+1] = Temp
# End of If
# End of Inner Loop J
# End of Outer Loop I
Step 8 : [Stop]
End
Page 13 of 18
LIST DATA TYPE
Insertion Sort:
Insertion sort is a method of sorting the given sequence not by exchanging the
value, but it is used to sort the value by inserting the value at it’s appropriate
position. In this sorting method very first element of the list is treated as a
sorted element and then next time next value is compared with previous value
and according to it’s position it get inserted at appropriate position.
Example.
12 26 7 80 38 56
This is unsorted data. Now sorting it through Insertion sort, the very first
element is treated as sorted element. Hence in Pass-1 value will be as :
Page 14 of 18
LIST DATA TYPE
Pass- 1 Action
12 26 7 80 38 56 No Action, as first element is
treated as sorted element
Pass- 2 Action
12 26 As 26 is greater than 12
hence the no 26 will get
inserted after 12 .
Pass- 3 Action
7 12 26 As 7 is smaller than 12, it get inserted before 12
Pass- 4 Action
7 12 26 80 As 80 is greater no, hence it get inserted at 4th
position only.
Pass- 5 Action
7 12 26 38 80 As 80 is greater no, hence it
get inserted at 4th position
only.
Pass- 6 Action
7 12 26 38 56 80 Final value in 6th pass is
sorted
As in each pass only one insertion is occurring , so Insertion sort will take
Maximum ( N) pass to sort the elements.
Page 15 of 18
LIST DATA TYPE
Write an algorithm to sort the list by using Insertion Sort.
Step 1 : [ Start]
Step 2 : [ Loop ]
For I = L to U
Step 3 : [ Compute]
temp=lst[I]
Step 4 : [ Re – Initialize]
J=I-1
Step 5 : [ Loop]
while lst[J]> temp and J >=0 :
Step 6 : [Compute]
lst[J+1]= lst[J]
Step 7 : [ Re – Initialize]
J=I-1
Page 16 of 18
LIST DATA TYPE
alst=[]
n=int(input(" Enter how many value you want in the list "))
for i in range(1,n+1):
alst.append(p)
print("\t",alst)
L=len(alst)
for j in range(0,L-1):
if(alst[j]>alst[j+1]):
print("\t",alst)
Page 17 of 18
LIST DATA TYPE
Write a Program in Python to sort a List by using Insertion Sort Method.
alst=[]
n=int(input("\t Enter how many value you want in the list "))
for i in range(1,n+1):
p=eval(input("\t Enter value = "))
alst.append(p)
print (" \t The List items are as .........\n")
print("\t",alst)
Page 18 of 18