You are on page 1of 5

LISTS TUPLES DICTIONARY

Mutable Immutable Mutable


 Elements can be changed using  Elements can’t be  Elements can be changed using keys.
index changed(except in  Key should be unique.
 It is sequence of elements or packing and unpacking).  It is an unordered set of elements
ordered elements enclosed in [ ]  But memory address will with {keys:values}
 Elements inside the list can be of vary even though  Key should be an immutable
any datatype elements are packed or datatype, where as value can be of
unpacked. any datatype.
 It is sequence of
elements or ordered
elements enclosed in ( )
or not enclosed with ( )
but it’s mandatory to be
separated by comma.
 Elements inside the tuple
can be of any datatype
Eg: [3,”n”,(56,),{7},{1:”l”}] Eg: ([3],”n”,(5,),{7},{1:”l”},6) Eg:{1:3,(1,2):”r”,”h”:[4,5]}
CREATING AN EMPTY LIST: CREATING AN EMPTY TUPLE: CREATING AN EMPTY DICTIONARY:
J=[ ] H=( ) N={ }
K=list( ) B=tuple( ) P=dict( )
CREATING A SINGLE OR MULTIPLE CREATING A SINGLE OR CREATING A SINGLE OR MULTIPLE
ELEMENT LIST: MULTIPLE ELEMENT TUPLE: ELEMENT DICTIONARY:
U=[6] Q=(3,) B={1:”J”}
V=[33,] W=3, H={“A”:{3,4},(13,):44}
W=[66,”H”,8.9,(33,)] S=(1,[“U”],{99},)
T=89,8.3,”H”
ACCESSING THE LIST: ACCESSING THE TUPLE: ACCESSING THE DICTIONARY
T=[2,3,(9,7)] T=[2,3,(9,7)] T={1:”F”,(1,2):34,”G”:[66,44]}
>>>T[1] >>>T[1] >>>T[1]
>>>T[-2] >>>T[-2] >>>T[(1,2)]
>>>T[2][0] >>>T[2][0] >>>T[“G”]
>>>T[2][1] >>>T[2][1] >>>T[G]
>>>T[2] >>>T[2] >>>T[1,2]
>>>T[6] >>>T[6] >>>T[6]
# IF THE INDEX SPECIFIED IS NOT # IF THE INDEX SPECIFIED IS # IF THE INDEX SPECIFIED IS NOT
AVAILABLE THEN IT WILL RAISE AN NOT AVAILABLE THEN IT WILL AVAILABLE THEN IT WILL RAISE AN ERROR
ERROR RAISE AN ERROR
SLICING: SLICING: SLICING:
M=[4,[6,],{7},4.4,”D”,(66,)] M=(4,[6,],{7},4.4,”D”,(66,)) Slicing cant be done for dictionary, instead
>>>M[0:1] >>>M[0:1] accessing is explained below
>>>M[1:1] >>>M[1:1] D={1:”F$”,(2,4):88}
>>>M[1:2] >>>M[1:2] >>>D[1]
>>>M[2:] >>>M[2:] >>>D[2]
>>>M[:2] >>>M[:2] >>>D[(2,4)]
>>>M[1:-1] >>>M[1:-1] >>>D[(2,5)]
>>>M[-2:0] >>>M[-2:0] K={1:77,D[0:1]:”NEW”}
>>>M[::-1] >>>M[::-1] >>>K[1]
>>>M[::2] >>>M[::2] >>>K[D[0:1]]
>>>M[::3] >>>M[::3] >>>K[D[0]]
>>>M[::-2] >>>M[::-2] >>>K[D]
>>>M[0:11] >>>M[0:11] # IF THE INDEX SPECIFIED IS NOT
>>>M[2:11:3] >>>M[2:11:3] AVAILABLE THEN IT WILL RAISE AN ERROR
>>>M[0::2] >>>M[0::2]
>>>M[:-1:2] >>>M[:-1:2] SAY VALID OR NOT
>>>M[:6:2] >>>M[:6:2] >>> V[B[2:3]]={89:67}
>>>T[6] >>>T[6] >>> V[B[2:3]][89]
# IF THE INDEX SPECIFIED IS NOT # IF THE INDEX SPECIFIED IS
AVAILABLE THEN IT WILL RETURN THE NOT AVAILABLE THEN IT WILL
OUTPUT AS [ ] RETURN THE OUTPUT AS ( )

OPERATORS : OPERATORS : OPERATORS(no specific operators are there


Concatenation + : Concatenation + : for dictionary):
Both operands must be a list. Both operands must be a tuple. OPERATIONS ON DICTIONARY:
Used to join two or more lists Used to join two or more tuples Adding element in to a dictionary:
Eg:[34,55]+[4]=[34,55,4] Eg(34,55)+(4)=(34,55,4) H={11:”U”,22:”CAN DO IT”}
[5,(67),6.7]+[22]=[5,(67),6.7,22] (5,(67),6.7)+[22]=(5,(67),6.7,22) H[33]=”PROVE URSELF”
>>>H
Replication * : Replication * : >>>{11:”U”,22:”CAN DO IT”,33:”PROVE
One operand should be list and One operand should be tuple URSELF”}
another must be an integer and another must be an integer
Used to replicate the given lists Used to replicate the given lists Updating element in to a dictionary:
EG: [23,45]*2=[23,45,23,45] EG: (23,45)*2=(23,45,23,45) >>>H={11:”U”,22:”CAN DO IT”}
>>>H[11]=”we”
Membership in,not in: Membership in,not in: >>>H
Operands can be of any datatype Operands can be of any >>>{11:”WE”,22:”CAN DO IT”}
It returns the output as False if the datatype
searched element is not in the list else It returns the output as False if Deleting an element from a dictionary a
it returns True the searched element is not in dictionary:
EG: 2 in [23,32] the list else it returns True H={11:”U”,22:”CAN DO IT”}
(9,) in [6,(9,)] EG: 2 in (23,32) del H[22]
Relational operator: >>>H
Relational operator: == and != works if both the >>>{11:”U”}
== and != works if both the operands operands are different or same Example 2:
are different or same format format >>>K={“A”:1,(7,8):99}
<,>,<=,>= works only if both the <,>,<=,>= works only if both the >>>K.pop((7,8))
operand are in same format operand are in same format >>>99
[3,4,[6,],(90,)]>[3,45] (3,4,[6,],(90,))>(3,45) >>>K
[3,4,(98,67)]==[4,(89),{90}] (3,4,(98,67))==(4,(89),{90}) {“A”:1}

FUNCTIONS: FUNCTIONS: FUNCTIONS:


1. len() 1. len() 1. len()
2. count() 2. count() 2. min()
3. index() 3. index() 3. max()
4. append() 4. min() 4. sum()
5. insert() 5. max() 5. sorted()
6. extend() 6. sum() 6. items()
7. reverse() 7. sorted() 7. keys()
8. sort() 8. tuple() 8. fromkeys()
9. min 9. values()
10.max 10.get()
11.sum 11.update()
12.pop() 12.set default()
13.remove() 13.copy()
14.del 14.pop()
15.clear() 15.popitem()
16.list() 16.clear()
17.dict()

You might also like