PYTHON- LISTS
PYTHON- LISTS
Entire list can be printed by just using the name of the, list
'saw', 'elbAl
l= [(Able, 'was', '", 'ere', "",
print(|)
usins
elements in a list can be accessed index
Like strings, individual The
also known as sequence types.
indices. Hence, they are
value starts from 0.
print(animals[1), ages[3])
ike strings, lists can be sliced,
print(animals[1:3))
print(ages|3:))
Looping in Lists
If we wish to process each itenm in
the list, we should be able to
iteratethrough the list. This can be done usingawhile or for
loop.
animals = [(Zebra', "Tiger, 'Lion', Jackal',
# using while loop 'Kangaroo']
i=0
while i< len(animals):
print(animals[il])
it=1
#using more convenient for loop
for a in animals :
print(a)
concatenated
(appended) at the en
Concatenation - One list can be
shown below:
of another as
16, 17]
Ist = [12, 15, 13,23, 22,
Ist = Ist + (33, 44, 55) 13, 23, 22, 16,17, 33, 44,55)
# prints [12, 15,
print(lst)
create a new Iist.
Merging -Two lists
can be merged to
50}
Istl = (10, 20, 30, 40,
i enptylist
isi? = Istl, i5t2 refer to difterent lists
ist2 = st2 + Iti
/
# prints (10, 20, 30, 40, 50}
print(Ist1)
pint(ist2)
#prints |10, 20, 30, 40, 50)
ist1<0)= 100
printlst2[Ol, Ist2j0) # prints 100, 10
chapter 8: Lists
103
num1 = 10
num2 = 10
s1 ='Hi
$2 = 'Hi'
print(num1 is num2) # printsTrue
print(sl is s2) # prints True
considered to be False:
following values are
Also note that the
None
Number equivalent to
zero: 0, 0.0, Oj
list andtuple:', "",()0
Empty string,
Empty set and dictionary: {}
Lists
Using Built-in Functions on
Many built-in functions
can be used with lists.
items in the list
# return number of
len(lst) the list
return maximum element in
max(lst) # the list
return minimum element in
min(Ist) #
elements in the list
# return sum of all
sum(lst) any element of Ist is
True
# return True if
any(lst) Ist are True
#return True if all elements of
all(lst) slice or entire list
# deletes element or
del( ) list, lst renmains
unchanged
# return sorted
sorted(lst)
reversed(lst) # used for reversing Ist
sorted(0 and
last 3, other functions are self-explanatory.
Except the usage
discussed in section after next. del( ) function's
reversed() are
is shown below:
L i s t M e t h o d s
Any list is an object of type list. Its methods can be accessed using
the syntax Ist.method( ). Usage of some of the commonly used
methods is shown bèlow:
original
converted into a list using
reverseiterator object which has to
list_
Iistl )toget a reversed list.
shown below:
peversal is also possible usinga slicing operation as
4)
Ist = [10, 2, 0, 50,
print(Ist[:-1]) # prints [4, 50, 0, 2, 10]
List V a r i e t i e s
a=[1, 3, 5, 7,9]
b= (2, 4, 6, 8, 10]
c= [a, b]
print(c[0)[O], c[1][2]) # Oth element of Oth list, 2nd ele. of 1st list
S= 'Hello'
I= [*s]
print() #outputs ['H", 'e', '",'", 'o']
x= (1, 2, 3, 4)
y= |10, 20, *x, 30)
print(y) # outputs [10, 20, 1, 2, 3, 4, 30]
chapter 8: Lists 107
Stack is a last in first out (LIFO) list, 0.e., last element that is added to
thelistis the first element that is removed from it.
P</ PiogtOms
Problem 8.1
on a list of names.
Perform the following operations
'Alka'
-'Anil', Amol', 'Aditya', 'Avi,
-Createa list of 5 names
-Insert a name 'Anui' before 'Aditya'
Appenda nameZulu'
Delete 'Avi' from the list
Replace 'Anil' with 'AnilKumar'
1 0 8
Let Us Python
all| the names in the list
Sort
- reversed. sorted list
.Print
PrOgram
C r e a t e
a list of 5 names
#names = ['Anil', 'Amol', 'Aditya', 'Avi", 'Alka')
print(nanes)
print(names)
#
delete 'Avi' from the list
names.remove('Avi')
print(names)
Output
{'Anil, 'Amol','Aditya', 'Avi', 'Alka']
['Anil', 'Arnol, 'Anu', 'Aditya'. 'Avi', 'Alka)
|'Anil', 'Amnot, 'Anuj', 'Aditya', 'Av', "Alka', Zulu')
l'Anil, 'Arnol', 'Anuj,'Aditya', 'Alka', 'Zulu')
lAnilkumar, 'Amol', 'Anuj', 'Aditya', 'Alka', "Zulu']
I'Aditya', 'AIka', 'Amol!, 'Anilkurnar', 'Anuj', Zulu')
Chopter & Lists 109
problem8.2
print(a)
#add prime numbers 11, 17, 29 at the beginning of the combined list
a=(11, 17, 29] + a
print(a)
print(a)
delete
the list
#
del a
Output
(1, 3,5, 7, 9]
10]
|2, 4, 6,8, 6, 8, 101
I1, 3, 5, 7, 9, 2, 4, 8, 10]
11. 17, 29, 1, 3, 5, 7, 9, 2, 4, 6,
13
200, 300]
[11, 17,29, 1, 3, 5, 7, 9, 2, 4, 100,
Problem 8.3
structure. Stack is a Last In
Write a program to implement a Stack data
at the
First Out (LIFO) Iist in which addition and deletion takes place
same end.
Program
outpur
30, 40, S0)
20,
p r o b l e m8 . 4
to
p r o g r a m implementa Queue data structure. Queue is a
First
a
(FIFO) liist, in which addition takes
Write
the queue.
the
Program
i n p o r tc o l l e c t i o n s
=collections.deque( )
gappend('Suhana')
qappend('Shabana')
q.append('Shakila')
qappend('Shakira')
qappend(Sameera)
print(q)
print(q.popleft( ))
print(q.popleft( ))
print(g.popleft( ))
print(a)
Output
'Sameera'])
'Shabana'.
'Shakila' 'Shakira',
Uequel['Suhana'.
Suhana
Shabana
Shakila
deque{['Shakira', 'Sameera'])