Part 6- List and Dictionary
List
List
is a collection of similar or different types of data which are separated by
commas and enclosed using square brackets.
n=[1,2,3,4,5]
m=[“CSS”,
“Python”, “Scratch”]
print(n)
print(m)
Output:
[1,
2, 3, 4, 5]
['CSS',
'Python', 'Scratch']
Example
2:
n=[1,2,3,4,“CSS”,"Python",5]
print(n)
Output:
[1, 2, 3, 4, 'CSS', 'Python', 5]
Example
3:
n=[1,2,3,4,“CSS”,"Python",5]
print(n[0])
print(n[1])
print(n[5])
Output:
1
2
Python
Built in List
function
1.
len()
it
is used to count the number of items in string, list, tuple and set,
syntax:
len(list/string)
Example
4:
n
= ["CSS", "Python"]
print(len(n))
Output:2
Example
5:
n
= "Python"
print(len(n))
Output:
6
n
= "Kathmandu Nepal"
print(len(n))
Output:
15
2.
append
it
is used to add data at the end of the list.
Syntax:
list_name.append(data)
Example
6:
m=["CSS",
"Python", "Scratch"]
m.append("HTML")
print(m)
Output:
['CSS', 'Python', 'Scratch', 'HTML']
3.
insert
it
is used to add data in the specified position.
Syntax:
list_name.insert (position, data)
Example
7:
m=["CSS",
"Python", “Scratch”]
m.insert(2,"HTML")
print(m)
Output:
['CSS', 'Python', 'HTML', 'Scratch']
m=["CSS",
"Python", “Scratch”]
m.insert(3,"HTML")
print(m)
Output:
['CSS', 'Python', 'Scratch', 'HTML']
4.
clear()
it
is used to remove all the data from a list.
Syntax:
list_name.clear()
Example
8:
m=["CSS",
"Python", “Scratch”]
m.clear()
print(m)
Output:[]
5.
sum
It
is used to add all the numeric data of the list.
Syntax:
sum(list name)
Example
9:
m=[10,20,30,40]
print(sum(m))
Output:
100
6.
Count
It
is used to count occurrence of given
data in the list.
Syntax:
list_name.count(data)
Example
10:
num=[1,2,3,2,1,2,3,2,2,3]
print
("number of occurrence", num.count(2))
Output:
number of occurrence 5
7.
min
It
is used to return smallest number in the list.
Syntax:
min(list name)
Example
11:
num=[34,54,33,12]
print
("smallest number", min(num))
Output:
smallest number 12
8.
max
It
is used to return greatest number in the
list.
Syntax:
max(list name)
Example
12:
num=[34,54,33,99,12]
print
("greatest number", max(num))
Output:
greatest number 99
9.
reverse
it
is used to reverse the order of the list
Syntax: (list name).
Example
13:
num=[34,54,33,12]
num.reverse()
print
("Reverse order", num)
Output:
Resverse order [12, 33, 54, 34]
10. del
it
is used to delete data from the list.
Syntax:
del list_name[index]
Example
14:
num=[34,54,33,12]
del
num[2]
print
(num)
output:
[34, 54, 12]
Worked Out Examples
number=[100, 11, 4, 45,
54, 93,200,65]
for n in number:
if n % 2==0:
print(n, end=" ")
Output: 100 4 54 200
2. Write a program to
input ten numbers from the user and store them in the list. The program should
then display the greatest and smallest number.
num = []
for x in range(10):
n = int(input("Enter number: "))
num.append(n)
g = max(num)
s = min(num)
print(f"Greatest
number in the list is {g}")
print(f"Smallest
number in the list is {s}")
3. Write a program to
store five numbers in the list. The program should then display the sum and
average of the numbers.
numbers = []
for i in range(5):
n = int(input("Enter number: "))
numbers.append(n)
s = sum(numbers)
avg = s / 5
print("Sum is
=", s)
print("Average
=", avg)
4. Write a program to
store ten numbers in the list. The program should then count the even and odd
numbers separately.
L = []
even = 0
odd = 0
for i in range(10):
n = int(input("Enter number: "))
L.append(n)
for x in L:
if x % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("Even numbers
in the list:", even)
print("Odd numbers
in the list:", odd)
Book exercise
page 287
numbers = []
n =
int(input("Enter number: "))
numbers.append(n)
greatest = max(numbers)
print("Greatest number is:", greatest)
2.Write a program to store five numbers in the list. The
program should then display the product of all the numbers.
numbers = [ ]
product = 1
for i in range(5):
n =
int(input("Enter number: "))
numbers.append(n)
product = product
* n
print("Product of all numbers is:", product)
4. Write a program to reverse the list in Python
L = [1, 2, 3, 4, 5]
L.reverse()
print("Reversed list:", L)
6. Write a program to input ten
numbers from the user and store them in the list. The program should then count
the even and odd numbers separately.
L = [ ]
even = 0
odd = 0
for i in range(10):
n =
int(input("Enter number: "))
L.append(n)
for x in L:
if x % 2 == 0:
even = even +
1
else:
odd = odd + 1
print("Even numbers:", even)
print("Odd numbers:", odd)
Dictionary
Dictionary is a built in data type which is mainly used for mapping purpose. It stores data in key value pairs, one being the key and the other corresponding pair element being its value.
Built in Dictionary Function
1.
clear()
It
is used to remove all the data from dictionary.
Syntax:
dictionary_name.clear()
Example
1:
employee={"100":"Ram",
"201":"Hari", "305":"Shyam"}
employee.clear()
print(employee)
Output:{}
2.
get()
It
is used to obtain the value linked to a particular key in the dictionary.
Syntax:
dictionary_name(get(key name))
Example
2:
employee={"100":"Ram",
"201":"Hari", "305":"Shyam"}
print(employee.get("100"))
print(employee.get("204"))
Output:
Ram
None
It is used to return the key from the
dictionary.
Syntax:
dictionary_name.keys()
Example
3:
student={"Name":"Ram",
"Age":"14", "Address":"Kathmandu"}
print(list(student.keys()))
Output:
['Name', 'Age', 'Address']
It
is used to return value from the dictionary.
Syntax:
dictionary_name.values()
Example
4:
student={"Name":"Ram",
"Age":"14", "Address":"Kathmandu"}
print(list(student.values()))
Output:
['Ram', '14', 'Kathmandu']
It
is used to remove data from the dictionary.
Syntax:
del dictionary_name[key]
Example
5:
capitals={
"Nepal":"Kathmandu",
"France":"Paris",
"Bangaladesh":"Dhaka"
}
del
capitals["France"]
print(capitals)
Output:
{'Nepal': 'Kathmandu', 'Bangaladesh': 'Dhaka'}
The
value of dictionary can be changed by referring to its key.
Example
6:
student={"Name":"Ram",
"Age":"14", "Address":"Kathmandu"}
student["Address"]="Patan"
print(student)
Output:
{'Name': 'Ram', 'Age': '14', 'Address': 'Patan'}
student={"Name":"Ram",
"Age":"14", "Address":"Kathmandu"}
student["address"]="Patan"
print(student)
Output:
{'Name': 'Ram', 'Age': '14', 'Address': 'Kathmandu', 'address': 'Patan'}
It
is used to count the number of key value pair in the dictionary/
Syntax:len(dictionary_name)
Example
8:
student={"Name":"Ram",
"Age":"14", "Address":"Kathmandu"}
print(len(student))
Output:3
Book exercise Page number 287
5. Write a program to
find the sum of values in a dictionary.
d ={"x:":100,"y":200,"y":200,"z":300}
sum=0
for i in
d.values():
sum=sum+i
print("Sum of
values are", sum)
Output: Sum of values are 600
6. Write a program to
check if the value 50 exists in the given dictionary.
data = {"a":
25, "b": 20, "c": 300, "d": 50}
for i in data.values():
if i == 50:
print("50 is present in a
dictionary")
Output: 50 is present in
a dictionary
3. Write a program to find the sum of five values in a dictionary. (Assume the value)
data = {
"a": 10,
"b": 20,
"c": 30,
"d": 40,
"e": 50
}
total = sum(data.values())
print("Sum of values:", total)
Output: Sum of values: 150
5. Write a program to delete a list of keys from a dictionary.
data = {
"a": 10,
"b": 20,
"c": 30,
"d": 40,
"e": 50
}
keys_to_delete = ["b", "d"]
for key in keys_to_delete:
if key in data:
del data[key]
print("Updated dictionary:", data)
Output: Updated dictionary: {'a': 10, 'c': 30, 'e': 50}
Difference between List
and Dictionary
|
List |
Dictionary |
|
List is an ordered
collection of heterogeneous types of data. |
Dictionary is an
unordered collection of data in a key: value pair form. |
|
List uses square
brackets [ ] |
Dictionary uses curly
braces {} |
|
Items have a defined
order, which will not change. |
Items do not have a
defined order |
|
List data can be
accessed by index, starting from 0. |
Dictionary data can be
accessed using keys. |
|
List allows duplicate
or repeated items. |
Dictionary does not
allow duplicate keys but values can be duplicated or repeated |
|
Example student =
["Ramesh", "Suresh", "Bimal",
"Nabin"] |
Example student {"name":
"Ramesh", "Age" 30} |