List, Tuple, and Dictionaries
List, Tuple, and Dictionaries
PYTHON
[] empty list
List in Python are stored as individual characters in contiguous locations, with two-way
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 :
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()
COUNT()-This function returns the count of the item passed as a parameter. Syntax :
list.count(<item>)
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 :
2. Adding elements to a dictionary: A new element can be added in the dictionary but it
must not exist in dictionary and must be unique . if the key already exists the this
statement will change the value of existing key and no new entry will be added.
3. Deleting elements from a dictionary :There are two methods fo deleting elements from
a dictionary.
4. Checking for a existence of a key:The membership operator in and not in work with
dictionary to check the existence of a element.
MEMORY MAP