DSA using Python
Array and list
Saurabh Shukla (MySirG)
- Agendas
① Array and list
② array module
③ Creating array
⑥ type codes
⑤ array methods
⑥ list
⑦ Creating list object
list
⑧ methods of
⑨ built-in methods
④ Humpy
Arrayandlistbuikt.in
module buititins
There several classes int float Str list
are , , , ,
range, dict set
defined in built-in module tuple , , ,
bytes Object Exception
, ,
e - ✓
Another module array
array is not a built-in data structure
and therefore need to be imported
array
-
This module defines object type
an
which can efficiently represent an
basic values : characters,
array of
point numbers
integers floating
.
and
are sequence types
Arrays but
much like lists ,
behave very
arrays can have elements of limited
types .
-
Creating -
Astray
from array import *
code , [ elements ] )
array ( type
,
at =
type code C Type
signed
' '
b -
integer
:÷=÷÷÷:÷;÷
"
'
h
'
Unicode
:
character
unsigned integer
÷
signed integer
: unsigned integer
' '
G unsigned integer
floating point
Exampte
from array import *
'
i' [ 5,10 , 15,40] )
at =
array ( ,
range (4)
:
for i in
10 15 40
)
' '
5
i] , end =
( at [
print
arraymet.no#
append C)
Count C)
extend C)
fromlist C)
index C)
insert C)
pop C)
remove C)
reverse c)
to list C)
list
list is a class
list is mutable
are indexed
list elements
list is an iterable
( dynamic array )
list can
grow
list can contain different type elements .
array
{
• collection of same type elements
•
fixed size 0 1 2 34
size =3
I
•
indexed
Dynamic array
-
•
collection of same type elements
I 2 3
0
size 4
•
re sizable FFF
=
• indexed ↓ 45 6 7
0 I 2 3
size = 8
1-7--1%-1111
createlistobjectll-C.IO
,
20 30]
,
l2 = []
]
'
'
abc
13 = [ 10
,
5.7
,
methodsofit
append ci
clear C)
count C)
extend C)
index C)
insert C)
pop C)
remove C)
sort C)
reverse c)
buittinmetnods
ten C)
sum C)
Max C)
min C)
sorted C)
both
-
list and array are
growabk .
•
list can contain heterogeneous data
data
. array can contain homogeneous
IF
you want to perform
mathematical calculations ,then
you should use Humpy arrays
by importing Humpy package .
Otherwise use lists as it work in
similar and more flexible to
a way
work with .
Install
humpy
open and / terminal
& Pip install
humpy
> pips install humpy
tlumpy
import humpy as rip
a =
np array ( [ 42,3 ]
.
)
print (a)
to = np array
. ( [ [ 11213] , [ 1920,303] )
print (b) ← 2d
array
< =
np .
array ([ [ 42133
,
[ 10,203 ] )
print (C) ←
Id array
with two list type
elements