11computer Science-List, Tuple and Dictionaries-Notes
11computer Science-List, Tuple and Dictionaries-Notes
Computer Science
Notes
LIST – A list is a standard data type of python that can store a sequence of values
belonging to any type. List is mutable. You can change elements of a list in place.
Following are some lists in python:
[] empty list
0 1 2 3 4 5
P Y T H O N
-6 -5 -4 -3 -2 -1
LIST OPERATORS –The various operators that can be used to manipulate list in
multiple ways :
>>>l1=[1,2,3]
>>>l2=[4,5]
>>>l1+l2
>>>[1,2,3,4,5]
List Replication operator (*) – This operator needs two type of operands. A list
and a number . The number operand tells the number of times list is to be repeated.
>>>l1=[1,2]
>>>l1*2
>>>[1,2,1,2]
Membership Operators –There are two membership operators for list. These are
in and not in. Both membership operators require that both operands used with
them are of list type.
List Slices – It refers to a part of the string containing some contiguous characters
from the list same as string.
List Functions– Python also offers many built-in functions and methods for list
manipulation. These can be applied to list as per following syntax :
Syntax : list.append(<item>)
INDEX() - It returns the index of first matched item from the list.
Syntax : list.index(<item>)
Syntax : list.extend(<list>)
Syntax : list.pop(<index>)
REMOVE()-It is used to remove the first occurrence of given item from the
list.
Syntax : list.remove(<value>)
CLEAR()-It is used to remove all the items from the list and the list
becomes empty.
Syntax : list.clear()
Syntax : list.count(<item>)
Syntax: list.reverse()
SORT()-It will sort the items of the list in increasing order by default.
Syntax : list.sort()
MEMORY MAP
LIST FUNCTIONS
index( ) append( )
extend( ) insert( )
pop( ) remove( )
clear( ) count( )
reverse( ) sort( )
List Manipulation :
1. Updating elements from the list – By assigning new value to the element of
the list through its index will change an element. Ex:
>>>l1 =[1,2,3]
>>>l1[2] = 4
>>>l1
[1,2,4]
2. Deleting elements from the list – the def statement is used to remove an
individual item, or to remove all items identified by a slice.
>>>l1=[10,12,13,14]
>>>del l1[2]
l1
[10,12,14]
3. Slicing the lists – Like string a subpart of the list will be displayed.
>>>l1=[10,12,14,20,22,24,30,32,34]
>>>s=l1[3 :-3]
s
[20,22,24]
TUPLE
() empty tuple
1.The len() method – It returns length of the tuple, i.e. ,the count of elements
in the tuple.Ex:
>>>tp=(1,2,3)
>>>len(tp)
3
2.The max() method – It returns the element from the tuple having
maximum value.Ex:
>>>tp=(10,5,78)
>>>max(tp)
78
3.The min() method – It returns the element from the tuple having minimum
value. Ex:
>>>tp =(9,3,1)
>>>min(tp)
1
4.The index() method – It returns the index of an existing element of a
tuple.Ex:
>>>t1=(3,4,5,6)
>>>t1.index(5)
2
5.The count() function – This method returns the count of a member
element in a given tuple. Ex:
>>>t1=(2,3,4,5,6,2,7,8,2)
>>>t1.count(2)
3
6.The tuple() method – It is constructor that can be used to create tuples
from different types of values.
>>>t= tuple(“abc”)
>>>t
(‘a’, ‘b’, ‘c’)
MEMORY MAP
TUPLE FUNCTIONS
len( ) max( )
index( ) min( )
count( ) tuple()
Tuple Operations :
2.Joining Tuple – The + operator, when used with two tuples, joins two
tuples.Ex:
>>>t1=(1,2,3)
>>>t2=(4,5)
>>>t1 + t2
(1,2,3,4,5)
4.Slicing the tuple – It gives a sub part of the tuple like list and string . Ex:
>>>t1=(10,12,14,20,22,24,30,32,34)
>>>seq=t1[3:-3]
>>>seq
(20,22,24)
DICTIONARIES :
1.The len() method – This method returns length of the dictionary (the count
of elements) .Ex:
>>> emp = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
>>>len(emp)
3
2.The clear() method – This method removes all items from the dictionary
and the dictionary becomes empty.
>>> emp = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
>>>emp.clear()
{}
3.The get() method – With this method we can get the item with the given
key.
>>> emp = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
>>>emp.get(‘age’)
24
4.The items() method – This method returns all of the items in the dictionary
as a sequence of(key,value) tuples .
emp = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
mylist=emp.items()
for x in mylist :
print(x)
the output will be like this:
(‘salary’ ,10000)
(‘age’ ,24)
(‘name’, ‘john’)
5.The keys() method – This method returns all of the keys in the dictionary
as a sequence of keys.
>>> emp = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
>>>emp.keys()
>>>[‘salary’ ,’age’ ,’name’]
6.The values() method - This method returns all of the values in the
dictionary as a sequence (a list).
>>>emp.values()
>>>[10000, 24, ‘john’]
7.The update() method – This method merges key:value pairs from the new
dictionary into the original dictionary, adding or replacing as needed . The
items in the new dictionary are added to the old one and override any items
already there with the same keys.
This method returns all of the keys in the dictionary as a sequence of keys.
>>> emp1 = {‘salary’ : 10000 , ‘age’ :24 , ‘name’ : ‘john’ }
>>> emp2 = {‘salary’ : 20000 , ‘age’ :25 , ‘name’ : ‘riya’ }
>>>emp1.update(emp2)
>>>emp1
{‘salary’ : 20000 , ‘age’ :25 , ‘name’ : ‘riya’ }
MEMORY MAP
DICTIONARY FUNCTIONS
len( ) clear( )
keys( ) get( )
values( ) update( )
Dictionary Operations :