Professional Documents
Culture Documents
#2. List
#2. List
List
⑥ How to delete an
list ?
of the
to edit element
⑦ How an
the list ?
elements in
-
④ concatenation operator
④ Repetition operator
④ list of lists
methods
④ list object
④ list comprehension
④ user input
④ Mutability and I ashability
test
•
list is a class
a list is an iterable sequence
•
list is mutable
list is able
•
grow
•
list can store
heterogeneous data
•
list elements are indexed
H¥Ébt ?
If = [ 10 20 30 ] # Square brackets are
, ,
used to denote a
list
12 [ ]
empty test
=
# object
13
"
[
"
= 50
,
3-5
,
abc ] #
Heterogeneous elements
IEEE?
It = [ 50 20,80
, , 10,60 ,
40 ]
( lilo ] ) # 50
point
print (dl[ I ] ,ti[ 23)
# 2080
3 4 5
0 I 2
l-j528.GG
Concept of
-
negative Indexing
It = [ 50 20,80
, , 10,60 ,
40 ]
ÉÉiii 3 4 5
0 I 2
# 40
print ( ti [ ])- I
¥-1 60
print ( SI [ -2] )
Aging emÉ-oop
11 = [ 50
20,80 10,674° ]
0 I 2 3 4 5
€0120180110160140
, ,
for x in 11 :
)
' '
Csc end =
print ,
i -0
while ic=5 :
] , end y
'
print Ceci
-
.
I -1=1
tlowtodeleteanelementfsomthelist9.lt
= [ 50 ,
20
,
80
,
10
,
Go
,
go ]
-6
0
-5
I
-4
2
-3 -2
3 4
-
5
I
l-j528.GG
jyiyiyi.la#----
I 2 3 4
0
del di [ 2]
HIdi-H-ofthedisl-9.lt
= [ 50 20,80
, ,
10
,
Go
,
go ]
-6
0
-5
I
-4
2
-3 -2
3 4
-
5
I
l-j528.GG
ll [ 2) = 45
-3 -2 I
-4
-
- G -5
3 4 5
0 I 2
1-7-150120145110160/40
HÉE?
-3 -2 I
-4
-
-6 -5
It = [ 50 ,
20
,
80
,
10
,
Go
,
go ] 0 I 2 3 4 5
1-7-150120180110160/40
index
l1[63=70 # error
}
11 [ 53=70 Not
adding more
values at
but only updating
le [ 43=90 and 4
index 5
There are two standard ways to add elements
in the list :
append c)
}
① class
attributes of list
② insert C)
attributes
( value )
list Object append .
the list
the last element of
-3 -2 I
-4
-
-6 -5
Lt = [ 50 ,
20
,
80
,
10
,
Go
,
go ] 0 I 2 3 4 5
1-7-150120180110160/40
Al append (7-0)
to
-
i É -5-3--5-6
'
-
50120180110460140170ft
ÉÉiiii
It = [ 50 20,80
, , 10,60 ,
40 ] 0 1 2 34 5
-5 -4 -3 -2 -
I
-6
11 .
insert (-5,7-0) -7
o i 2 3 4 5 6
50/70120/8011016014--1
It
a ,
b
,
c = #
unpacking
number of variables in the left hand side
in the list .
9=5
b. = 6
C =
10
12 = [ a ,
b , c] 4-
packing
Built in methods
-
ten C)
length of specified
→
returns
iterable
mine )
→
returns min value element
Max 1)
→ returns Max value element
sum C) →
returns
sorted C)
sum
of elements
→ returns a sorted list
of elements .
listcsmethodle-lis.tt
) # I / = []
ls = list ( lo ) # d1=[I→ Error
11 = list ( 10120,30)
ll = list (
"
mysircr
"
)
ll list (
range (5) )
=
11 list ( [
10,20] )
=
at most
list C) method can take
one argument .
iterable
one argument →
compariscnoper.at#onIist
lI =
[ I
,
2
,
3 ]
12 =
[ 2
,
3
,
I ]
13 = [1 ,
2
,
3 ,
4 ,
5 ]
14 =
[1 , 2 3 ]
,
l I = =
l2 False
11 = = l 3 False
11 = = 14 True
el > 12 False
Concatenation Operator
11=2115,9] Dig
12=[2/3,1] 12¥
13 = It + d2
0 I 2 3 45
tf
11-1--12 → 11=11+12
Repetition Operator
ll=[ 2,5 ] 1%-5 5 6 7 8 9
0 I 2 3 4
11*5
21512151215/2/-1215--4
bistoflists
It = [ [ 1
, 3,5 ] ,
[ 2 , 1,8 ]
,
[ 5 4,4 ]
, ]
I 2
0
☐1☐|☐É%:
It [ o ] 11¥
lilo ] [2]
[ {? ¥
]
5
5 44
listobjectmethods
append c)
insert C)
remove C) → Remove first occurrence of specified
element error if the element
.
It
gives
is not in the list
present
pop C) → Remove and return last element
index number
optionally you can pass
to specific element
pop
.
of specified element
count C) → count number of occurrence of
specified element
listccmprehensian
are
•
immutable objects are not
changeable
. All immutable objects are hashable but
,
not all washable objects are immutable .
÷:÷
Yes No
Complex Yes NO
Stv
÷:
set
NO