0% found this document useful (0 votes)
15 views43 pages

Collection Datatype

Uploaded by

Michael Devasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views43 pages

Collection Datatype

Uploaded by

Michael Devasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Collection Data type

Till now we were


storing one data with a single
data type to a single variable

Now we want to store multiple data of same

or different data types so we use the collection


data type
Collection data type

four List
Tuple
different
set
types
Dictionary

How the data is stored depends on individual


properties
LIST
Properties of Lists

D List is a collection data type

2 List can store same or different types of


data

3 Inside the list each data is called element

4 Inside a list each element has an index value

the Indening and ve Indening can be used


5 Lists are ordered because they have Index

6 Lists are mutable after creation we can


replace add or delete

7 Lists can have duplicate elements

8 Lists are dynamic


Lists are represented in square brackets

e es es

The elements inside need to be separated


by comma's
list constructor
list C It is also possible to use the lists
Constructor when list
creating a new

list e ez ez

It has double round brackets

ex l 2 1 25 S 1.2

let different datatypes


Both Indening and slicing concepts can be
used here

ex list 1 apple banana orange

list 1 2

orange

list 1 o 2

apple banana

list C constructor can construct lists from


lists tuples and sets
Adding elements with replacing
For this we use Indening and slicing
To add single element in a particular Index

position we use Indening


ex

l 11 12 13 a b

if we want to add 100 to this list at snden

position of 3 we say
l 33 100
a'set replaced
d with 100
I Cll 12 13 too g

if 114 100 2001300


Just use commas instead of
brackets as memory leakage
happens
300
I l I 11 12 13 100 100 200

If we want to add element at a range of indene

positions we use slicing


122 43 40
Comma is mandatory as machine
is expecting iterable list of
elements

l 11 12 40 100 200 3003

when we are multiple elements and


accessing
replacing them it is not mandatory to replace
with same number of elements

If lesser elements are used to replace then


the other elements from the original list will
disappear
Adding elements without replacing
three insert C
list appends
methods extend C

List methods methods that can only be used


on lists
we use dot operator here It first checks

if list is there and then applies the method

Insert C method
Insert is a list method by using which we

can add single element without replacement


at a specified location
Here we need to specify the index where
element needs to be added and the element
to be added

insert Inden 9
Element to
value be added

h 11 12 100 200 300

l insert o a

h a 11 12 100 200 300


In Insert C the old elements are pushed
to the right to create space for the element

to be added

If we want to add element at the last

Inder position using ve Index t then


element cannot be pushed and we get
wrong list

l insert 1 b

l a 11 12 100 200 b 3003


wrong
Position X

Instead we need to use ve Indening


l insert S b

Insert can insert one element at a


only
time so if we want to insert multiple
elements we need to perform insert option

multiple no of times

append method

append C is a method by using which we can

add element without replacement at


a
single
the end of the list
Here the index value need not be specified
as the new element is appended at the end
of the list

ex l a 11 12 100 200 b 3003


l append it 25

l a 11 12 100 200 b 300 it 253


t
brackets are only
added for visualization

append can insert element at a time


only one

If we want to insert multiple elements append


needs to be applied multiple number of times

extend C method

This is a list method that can append n

of sequential nu databerelements without


replacement at the end of the current list

rare extend sequential data

sequential data this is a data that has sequence


of elements

Data type sequential data


Numeric data types X
like int great complex

strings

Collection data type

ex l o I 2 3 a

d extend 100 200,3007


these square
brackets are must

I O 1 2 3 a 100 200 3007


l extend abode

l o 1 2 3 a 100 200,300 a b c d e
T
extends as single elements
and not as a string

when we want to append another list to the

current list we use extend c


Deleting Removing elements from lists

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

specified indene from the list


Here Onder values need to be specified
del keyword can be used anywhere

pepo is only used in lists

If no Inder is specified in pop this method


removes the last item

ex le 11 12 13 100 200 3003


l Pop Co
what was removed
11 output gives
l 12 13 100 200 4007
mentioned
T nothing was
l pop
400 ie last element

l 12 13 100 2003

remove c method
remove 17 method is a list method by using
which we can remove an specified element

from the list

Here we
specify the element to be removed
and not the index of the element

will only remove the first instance of the


element
we can only remove one element at a time
ex l 11 12 13 100 11 400 123
e remove ID
h e 12 13 100 11 400 123
II
l remove

l 12 13 100 400 123

Clear C method
clears 7 method is mainly used to remove

all the elements from the list

i e to create an empty list


l clear C
empty list

empty list if the list exists but it does not

have any elements in the list

After clear the list still exists but it is empty


ex l 11 12 133
l clear C

l C
copying a list
we cannot copy a list simply by typing
list 2 list 1 x

because list 2 will only be reference view


a

to the list 1 and changes made in lists


will automatically be made in list 2

There are ways to make a copy one way


is to use a list method called copy c

Copy C method

copy c method is a list method that will copy


and create a new memory location so new

list is created
ext 1 61 12 13 12 1 is X

2 1 copy
2 11 12 13

we can also use


list method to copy
a list

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

3rd element is age 4th element is height

I Subhadra Bhupathi raju 42,170

bio data my name is 3 53 my age is 3


my height is 3
bio data format lo 1613 I 23,1233

my name is Subhadra Bhupathi raju my


is 42 my height is 170
age
Sort list Alphanumerically

Sort C method

list objects have a sort c method that will


sort the list alphanumerically ascending by
default
The sort list needs to be homogenous

homogenous same type of data

heterogenous different data types of data

ex
l 4 7 2 6 8

l sort C

l 2 4 6 7 8

If we want to sort in descending order we need

to use the key word argument


reverse True

l Sort reverse True

l 8 7 6 4,2

default False
by reverse

once the list is sorted it will automatically


stored So no need to do l l sort c
are sorted alphabetically with
Alphabets
to upper pref rence
letters
case

l a I d A e

l sort C

b EA a c d e

Complex cannot logically be sorted

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

Count C and Index methods can be applied

to both lists and tuples find c does not

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

lists can be created with single elements


ex l 1
list methods

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

D Tuple is a collection data type

2 tuples can store same or different types of


data

3 Inside the tuple each data is called element

4 Inside a tuple each element has an index value

the Indening and ve Indening can be used


5 tuples are ordered because they have Index

6 tuples are immutable After they


creation cannot
be changed

7 tuples can have duplicate elements

8 tuple are static


tuples are represented in round brackets

e ez ez

The elements inside need to be separated


by comma's
Tuple constructor
tuple C constructor can also be used to create
a new tuple
tuple er ez ez

It has double round brackets

Both Indexing and slicing concepts can be

used here to access single or sequential


elements

Count C and Indene methods can be used


on tuples
other tuple methods do not exist because

tuples are immutable

tuples cannot
replace
add
delete

If we try to edit tuple it will give error

If we want to create a single element

tuple the we cannot mention


the x
as python considers this as an integer
so we should write

E Gi
this comma is must

ex E Cle

Empty tuple is C

tuple to list can be done but


Converting
it is not a recommended practice
For this we can use lists constructor

tuple C constructor can construct tuples from


lists tuples and sets

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

or we will get an error

Instead we can use args C

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

1 sets is a collection data type

2 Sets can store same or different types of


data

3 Inside the set each data is called element

4 Elements do not have index value inside a

set

5 Sets are unordered because they do not have


Index

6 Sets are mutable no replace and unchangeable


sets items are unchangable meaning that
we cannot change the items after the set
has been created i e we cannot replace
But we can add new items and delete items
we cannot replace because we do not have

index to access that particular element

7 Sets will not allow duplicate values All

the duplicate values will be removed

8 Sets are mutable dynamic collections of


unique imm
elements table
Sets are represented in fewer brackets

e ez ez

The elements inside need to be separated


by comma's

we can use a set constructor sets to

create a set
set Cei eyes MYIciais
we cannot access items in a set
using
Index

Add Items once a set is created we cannot


add C change its items but we can

updates add items

add C method
Adde is a set method by using which we

can add a single new element inside a set

Yar add element to be added

we do not know at what position it will

be added
update C method
updates is a set method by using which
we can add multiple sequential data
elements at a time

The object inside the update method


does not have to be a set it can be

any other iterable object tuples lists

dictionaries etc

Sett update seta


set 1 update string
set 1 update list
set 1 update tuple
Remove stems we can remove an item in
remove c a set

discard C
clear C
del

remove C method

removes is a set method by using which


we can remove a single element at a time
If the item to remove does not exist
remove will raise an error

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

Code does not break

var discard a
we can use Pope method but since sets
are unordered and Pepe removes the

last element when Inder is not specified


we do not know which item is removed
before hand

e The output of the pep method is the

removed item

Clear C method
clear c is a set method that will remove

all the elements from a set and create


an
empty set

Var Clear C
Sete

Empty list
Empty tuple
set C Empty set
3 Empty dictionary

del keyword del keyword will delete the set

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

containing all the items from both sets

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

Any duplicates are removed

update C method
updates method when used between two
sets it inserts all the items from one set

to the other set

Set 1 Si Sa 533
Set 2 Sy Ss 563

Set 1 update Seta


S 52 53 Su Ss So

The update c does not create a new list


It deletes all duplicate values

Keep only Duplicates

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

Intersection update method

The intersection update method will keep only


the items that are present in both sets and

update the original with these values

This will not create a new set

Set 1 intersection update set 2

Set 1 53,543
set 2 intersection update sett

set 2

qsz.su

Keep All But wet the Duplicates

difference C method

method will keep only the elements


This difference c
that are not present in both sets

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

creates a new set

difference update C method The difference


update method will keep only the items that
are not present in both sets and update
the original set with these values

Set 1 difference update Seta

sett
5,523 E
setz difference update sett

set 2 S Sz

so so E

sets have single elements in it It


only
cannot have sequential data in it

i e sets cannot contain lists tuples etc


set methods

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

difference C Returns a set containing the difference


between two or more sets

Removes the items in this set that


difference
updates are also included in another

specified set

discard Removes the specified item

intersection Returns a set that is intersection


of two other sets

intersection Removes items in this set that are

update not present in other specified set s

is disjoints Returns whether two sets have a

intersection or not

is subset C Returns whether another set contains


this set or not

is superset C Returns whether this set contains


another set or not

Pop C Removes an element from the set

remove C Removes the specified element


Union C Return a set containing the union
of sets
update update the set with the union

of this set and others


Dictionary
Properties of Dictionaries

dictionary is a collection data type


D

2 dictionaries are used to store data values


in key value pairs

3 A dictionary is a collection which is ordered


Note before 3.6 version dictionaries were unordered

4 Inside the dictionary the key values are

our Inder's

5 Indexing concept is possible but slicing is


not
not possible as keys are
sequential

6 keys cannot be duplicate but values can

be duplicate If duplicate key is used then


the previous value in the key will be replaced

with the new value

7 dictionaries are mutable

keys are immutable but values are mutable

8 dictionaries are dynamic


I dy
we can create a dictionary using flower
brackets
Each element will have keys and values

value
key values key z 3

Key and value are separated by Colon

elements are separated by a comma s

keys and values can be any data type

Advised to use integer and string in the keys


It is illogical to use float and complex
values can be dictionary
anything including a

i e int float set list tuple dictionary


dicta constructor exists in python but it

is not used

dictionaries are mutable


This means that we can change add or remove

items after the dictionary has been created

To replace delete or add we can use Indexing


Here the Index would be the key
replace
value
Yar key I new

delete
del var keys

here both key and the corresponding value


will be deleted

add
Yar new key new value

If the mentioned key is non existing after


the check process then it will add the
new key and value to it New key is added
at the end

If key is already present the new value

will replace the old value


Dictionary methods

update c method
update c method is a dictionary method in which
we can replace or add multiple elements inside

a dictionary

when we use update method for dictionary


it can only take the input as dictionary

Var update key value keys values

If keys are same it will replace old values


with the new values

If keys are different it will add multiple


elements at a time

Pop C method
Pope method is used to remove a key that is

specified
Pop key here in den is key
Yar

Value will also be deleted along with the key


pop item method
The pep item c method removes the last inserted
item
Note In versions before 3.7 a random item is

removed instead

Yar Pepi tem

Needs to be used with caution

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

values in the form of a tuple list

Yar items c

key 1 valued keys values


I

clear C method

The clear c method will clear all the elements


and give an Empty dictionary

Var clear C

3 Empty dictionary

copy method

copy c method is used to create a new

dictionary by creating the copy of the old


one

dz die copy c

Assignment operator dz d will only create


a view of the original dictionary
Nested dictionary
A dictionary can contain dictionaries inside

this is called nested dictionaries

ex A dictionary nested inside another dictionary

For this the key 1 values The values here

will be another keys values so we need


double Indening to access the value

Var key keys value 233


Yar key keys values
Yar key keys values

when choosing a collection type It is useful


to understand the properties of that type
choosing the right type for a particular data
set could mean retention of meaning and
it could mean an increase in efficiency
or security
Dictionary methods

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 keys and value


g etc Returns the value of the specified
key
items C Returns a list containing a tuple
for each key value pair
Keys C Returns a list containing
dic ionary
keys
Pope Removes the element with the

specified key

popiteme Removes the last inserted


key value pair
set default C Returns the value of the specified
key If the key does not exist

insert the key with specified value

updated updates the dictionary with the

specified key value pair


values c Returns a list of all the values
in the dictionary
Lists Tuples sets Dictionaries
have Index have Index no Inder key is the index
ordered ordered unordered ordered version 37 7
Mutable Immutable partially mutable mutable
Allow Duplicates key cannot beduplicate
Allow Duplicates Do not allow duplicates value can be duplicate
Ce ez Cei ez d Ek evi Kiva 3
Ge ez 3
list C tuple C set C dict c
C TEE C ELIE Sete Empty set 3 Emptydictionary
Indening slicing Indu n skins
possible possible
Indening slicing slicing is not possible
not possible enduring is possible
Elements are in pairs
join C method string method
The method takes all items in an
join c

iterable and joins them in to one string

A string must be specified as the separator


String join Citerable
Input is any iterable object i e list set or
tuple

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

You might also like