Collection Datatype
Collection Datatype
four List
Tuple
different
set
types
Dictionary
e es es
list e ez ez
ex l 2 1 25 S 1.2
list 1 2
orange
list 1 o 2
apple banana
l 11 12 13 a b
position of 3 we say
l 33 100
a'set replaced
d with 100
I Cll 12 13 too g
Insert C method
Insert is a list method by using which we
insert Inden 9
Element to
value be added
l insert o a
to be added
l insert 1 b
multiple no of times
append method
extend C method
strings
ex l o I 2 3 a
l o 1 2 3 a 100 200,300 a b c d e
T
extends as single elements
and not as a string
del keyword
four method
pope
ways
removed method
Clearly method
del keyword
del keyword can remove a single element
from a specified index or a range of
elements or complete list
ext
l 11 12 13 100 200 300
del la
l 11 12 100 200 3003
del l 2 53
l 11 123
del l
his completely deleted
Pope method
Pepe method is a list method by using
which we can remove an element from a
l 12 13 100 2003
remove c method
remove 17 method is a list method by using
which we can remove an specified element
Here we
specify the element to be removed
and not the index of the element
Clear C method
clears 7 method is mainly used to remove
l C
copying a list
we cannot copy a list simply by typing
list 2 list 1 x
Copy C method
list is created
ext 1 61 12 13 12 1 is X
2 1 copy
2 11 12 13
l 11 12 13
2 list l
2 11 12 13
Exercise
Q create a list where inside the list istelemen
is first name 2nd element is last name
Sort C method
ex
l 4 7 2 6 8
l sort C
l 2 4 6 7 8
l 8 7 6 4,2
default False
by reverse
l a I d A e
l sort C
b EA a c d e
reverse C method
reverse C method in the list reverses all the
elements
ex
l a b d d
e reverse C
l d c 5 a
ex
l 5 7 6 4
l reverse
l 4 6,7 5
exist
Count C method
Count C method is used to count how many
number of times that particular element
exists in the list C J or Tuple c
l Count element
number of times it exists
Indene method
Indene method is used to know the index
of an element at the first instance go.int
left to right
l index element
Index at the first instance
Insert C
append C
extend C
remove C
pop C
clear C
Copy C
Sort C
reverses
Count C
Index C
Tuples
Properties of tuples
e ez ez
tuples cannot
replace
add
delete
E Gi
this comma is must
ex E Cle
Empty tuple is C
Tuple unpacking
If we want to unpack a tuple we assign it
to the no of variables that are equal to
the no of parameters
ex N Y Z E 1 2 3 4
3 32 3
The no of variables and elements have to match
R Y 3 1,2 3,9
My I 2 3 3,4
N y 3 1,2 3,4
x
Y
2,3 y
sets
Properties of sets
set
e ez ez
create a set
set Cei eyes MYIciais
we cannot access items in a set
using
Index
add C method
Adde is a set method by using which we
be added
update C method
updates is a set method by using which
we can add multiple sequential data
elements at a time
dictionaries etc
discard C
clear C
del
remove C method
Var remove a
discards method
discards method is a set method by using
which we can remove a
single element
at a time
the be removed does not exist
of item to
will raise error so the
discards net an
var discard a
we can use Pope method but since sets
are unordered and Pepe removes the
removed item
Clear C method
clear c is a set method that will remove
Var Clear C
Sete
Empty list
Empty tuple
set C Empty set
3 Empty dictionary
completely
Join sets C we use several ways to join two
union C or more sets in python
updates
union C method
It joins the two sets and returns a new set
set 1 S i 52 533 s s S
set 2 Su Ss 563
Jett Yet 2
Set 3 Set 1 Union set 2
S 52 53 Su Ss 563
update C method
updates method when used between two
sets it inserts all the items from one set
Set 1 Si Sa 533
Set 2 Sy Ss 563
Intersection method
The intersection 7 method will return a new set
that only contains items that are present in
both sets
set 1 S Sz Sz Sy
set 2 53 54 Ss So É
Set 3 Sett intersection seta
53 543
Set 1 53,543
set 2 intersection update sett
set 2
qsz.su
difference C method
Set 1 S S2 S3 543
Set 2 53 Sy 55 563
Set 3 set 1 difference Seta
set 2 Sa
Set 1
Si 523 LI
Set 3 Set 2 difference sett
Set 2 set 1
5 1563
sett
5,523 E
setz difference update sett
set 2 S Sz
so so E
Method Description
add C Adds an element to the set
clear C Removes all the elements from the
set
Copy C Returns a copy of the set
specified set
intersection or not
our Inder's
value
key values key z 3
is not used
delete
del var keys
add
Yar new key new value
update c method
update c method is a dictionary method in which
we can replace or add multiple elements inside
a dictionary
Pop C method
Pope method is used to remove a key that is
specified
Pop key here in den is key
Yar
removed instead
Keys C method
The keys C method will return a list of all
the keys in the dictionary
Yar Keys C
keys list
i e
key 1 keys keys
Values method
The values C method will return a list of
all the values in the dictionary
Yar values c
values list
ie valued valuer
Items C method
The items c method will return all the keys
Yar items c
clear C method
Var clear C
3 Empty dictionary
copy method
dz die copy c
method Description
Clears Removes all the elements from
the dictionary
Copy C Returns a copy of the dictionary
from keys Returns a dictionary with the
specified key
var
operator join
operator is better to be Empty or space.lu
l a 5 d d
join Cl
abcd
W join e
a b c d
join C will be used a lot in NLP