### Unit 1 - Lists and dictionary

CBSE Revision Notes

Class-11 Informatics Practices (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)

Lists and dictionary

In this section we’ll discuss lists and dictionary

Lists: Lists are the most versatile of Python's compound data types. A list contains items

separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One of the differences between them is that all the items belonging to a list can be of different data type. The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. Let’s understand this with an example

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

tinylist = [123, 'john']

print (list) # Prints complete list

print (list[0]) # Prints first element of the list

print (list[1:3]) # Prints elements starting from 2nd till 3rd

print (list[2:]) # Prints elements starting from 3rd element

print (tinylist * 2) # Prints list two times

print (list + tinylist) # Prints concatenated lists

Maximum number from the list program: There are 2 ways to do this program

1. Using inbuilt function called max
2. Using loops

We will do this program from both ways

Using inbuilt function

list=[10,8,99,22,11,5] #initializing list

maximum=max(list) #it will return maximum number from the list

print("Maximum number from the list is",maximum)

Using loops:

list=[10,22,1,98,2,43,100]

maximum=list[0] #putting the first value of list into variable maximum

for i in range(0, len(list)): #iterating through the list from 0 to total length of the list

if list[i]>maximum: #checking if next number is greater than the number saved

maximum=list[i] #if yes then put that value into maximum

print ("Largest number from the list is ",maximum) #print the value

Minimum number from the list: We’ll also do this program using both the methods

Using inbuilt function:

list=[10,8,99,22,11,5] #initializing list

minimum=min(list) #it will return minimum number from the list

print("minimum number from the list is",minimum)

Using loop:

list=[10,22,1,98,2,43,100]

minimum=list[0] #putting the first value of list into variable minimum

for i in range(0, len(list)): #iterating through the list from 0 to total length of the list

if list[i]<minimum: #checking if next number is smaller than the number saved

minimum=list[i] #if yes then put that value into minimun

print ("Smallest number from the list is ",minimum) #print the value

Mean of numbers program:

list=[2,44,12,53,22]

sum=0

for i in range(0,len(list)):

sum=sum+list[i] #it will add all the items from the list

mean=sum/len(list) #to find the mean sum of items is divided by total number of items

print("Mean of numbers is ", mean)

Linear search:

list=[11,22,3,112,98,45,1]

print("Enter number you want to search")

num=eval(input()) #user enters the number he wants to search

flag=False

for i in range(0,len(list)): #iterates through the list

#checks every element with the number user entered if its equal it will print the message

if list[i]==num:

print("Number found")

flag=True #flag will set to true and will break the loop

break

if flag==False: #if flag is still false means number is not found

Dictionary: Dictionary is a comma separated pair of key and value enclosed in curly braces. Each key is separated from its value by a colon [:]. Empty dictionary without any item is written with just two curly braces. Keys are unique in dictionary while values may not be. The values of a dictionary can be of any type. Key must be of immutable data type such as string, numbers or tuples. Let’s see an example of dictionary

dict= {‘Name’ : ’Mehak’, ‘Age’ : 15 ‘class’ : ‘10th’ }
print ("dict['Name']: ", dict['Name'])

print ("dict['Age']: ", dict['Age'])

Result of above example will be:

dict[‘Name’]: Mehak

dict[‘Age’]: 15

Basically these age name and class are keys and we can access the value associated with it using these keys.

Counting the frequency of elements in a list using a dictionary:

#initializing list
names = ['Physics', 'Chemistry', 'Physics', 'Biology', 'Math', 'Biology', 'Biology', 'Math', 'Chemistry']
d = {} #creating empty dictionary

for key in range(0,len(names)): #this loop will iterate through the list

count=0 #count will count the number of appearance of words

flag=False #flag will check if that word has already been counted or not

name = names[key]

for i in range(key, len(names)): #this loop will help us to compare the word with rest of the words in the list

if name not in d: #if name doesn't exist in the dictionary

d[name] = [] #it will add name to the dictionary

flag=True #this flag will indicate it is the first time that word has appeared

if name == names[i] and flag==True: #in this we'll check if name appears again in the list and is it for the ist time we are checking it

count+=1 #if it appears again count will increase

d[name]=count #and it will add count to that name

print(d) #displaying dictionary