Python (List & Tuple)
Python (List & Tuple)
L=[10,15,20,25,30,35,40,45,50]
print(L[:])
pro2012
print(L[2:6])
print(L[5:20])
print(L[2:-2])
print(L[-20:4])
print(L[0:15:2]) # include every 2nd elements
print(L[0:10:3]) # include every 3rd elements
OUTPUT:
print(L[2:15:4]) # include every 4th elements [10, 15, 20, 25, 30, 35, 40, 45, 50]
print(L[-1:-9:-2]) # from back [20, 25, 30, 35]
[35, 40, 45, 50]
-9 -8 -7 -6 -5 -4 -3 -2 -1 [20, 25, 30, 35, 40]
[10, 15, 20, 25]
10 15 20 25 30 35 40 45 50 [10, 20, 30, 40, 50]
[10, 25, 40]
0 1 2 3 4 5 6 7 8 [20, 40]
[50, 40, 30, 20]
Python sequence slice addresses can be written as L[start:end:step]
and any of start, stop or end can be dropped.
e.g. L[::3] is every third element of the sequence.
e.g.
LI= [10,25,35,8,20,45]
print (LI[::2]) # to display every 2nd element of the sequence
print (LI[::4]) # to display every 4th element of the sequence
print (LI[::3]) # to display every 3rd element of the sequence
OUTPUT:
[10, 35, 20]
[10, 20]
[10, 8]
LI= [10,4,6,25,35,8,100,20,45]
print (LI[::2]) # to display every 2nd element of the sequence
print (LI[::4]) # to display every 4th element of the sequence sssss
K=[‘aaa’,’bbb’,’ccc’,’ddd’]
To make a true copy (i.e. an independent list identical to the given list) use
following statement
G=list(K) # Now G and K are separate lists(called deep copy)
Making True Copy of a List
Assignment with an assignment operator(=) on lists does not make a
copy. Instead, assignment makes the two variables point to the one
list in memory (called shallow copy).
K=[‘aaa’,’bbb’,’ccc’,’ddd’]
e.g.
list=[1,2]
print('list before append:', list)
list.append(3)
print('list after append:', list)
Output
list before append: [1, 2]
list after append :[1, 2, 3])
NOTE :- extend() method can be used to add multiple item at a time
in list.eg - list.extend([3,4])
NOTE :- extend() method can be used to add multiple item at a time
in list
eg
list.extend([3,4])
OUTPUT
[1,2,3,4,5,6]
# Use of extend(),insert(),remove()
L=[1,4,2,3] [1, 4, 2, 3, 4, 5, 6]
M=[4,5,6] [4, 5, 6]
L.extend(M) [1, 4, 2, 100, 3, 4, 5, 6]
print(L) [1, 4, 2, 100, 3, 4, 5]
print(M) [1, 2, 100, 3, 4, 5]
L.insert(3,100) []
print(L)
L.pop() # because index no is not specified so delete last element
print(L)
L.remove(4)# to remove the first occuerence of the given element
print(L)
L.clear()
print(L)
L=[1,4,5,6,4,8,50]
print(L) OUTPUT
print(L.pop(5)) [1, 4, 5, 6, 4, 8, 50]
print(L) 8
print(L.pop()) [1, 4, 5, 6, 4, 50]
print(L) 50
print(L.remove(4)) [1, 4, 5, 6, 4]
print(L) None
L.clear(); [1, 5, 6, 4]
[]
print(L)
# use of count(),reverse(),sort()
LI=[1,2,3,4,5,4,6,7,4,8,2] pro2016
print(LI.count(4)) # to count no 4 present in list
print(LI.count(200)) output
LI.reverse() 3
print(LI)
0
LI.sort()
print(LI) [2, 8, 4, 7, 6, 4, 5, 4, 3, 2, 1]
LI.sort(reverse=True) [1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 8]
print(LI) [8, 7, 6, 5, 4, 4, 4, 3, 2, 2, 1]
print(max(LI)) 8
print(min(LI)) 1
l=list("students")
print(l)
k=('w','e','l','c','o','m','e')
l2=list(k) pro2009
print(l2)
l3=list(input('Enter list elements-'))#12345
print(l3)
l4=eval(input('Enter list to be added-'))#[1,2,3,4]
print(l4) Output
l4[2]=100 ['s', 't', 'u', 'd', 'e', 'n', 't', 's']
print(l4) ['w', 'e', 'l', 'c', 'o', 'm', 'e']
Enter list elements-12345
['1', '2', '3', '4', '5']
Enter list to be added-[1,2,3,4]
[1, 2, 3, 4]
[1, 2, 100, 4]
R=[2,4,6,8,10,12,14,16,18,20,22] del statement
print(len(R)) # 11
del R[4] # to delete the element at index no 4
pro2040
print(R) # [2, 4, 6, 8, 12, 14, 16, 18, 20, 22]
del R[2:6] # to delete the elements from index no 2 to 5
print(R) # [2, 4, 16, 18, 20, 22]
R.extend([40,50,60]) # to add specified elements at the end of existing list
print(R) # [2, 4, 16, 18, 20, 22, 40, 50, 60]
del R[0:10:4] # to delete every 4th occurrence elements of the list
print(R) # [4, 16, 18, 22, 40, 50]
del R[:2] # to delete elements at index no 0 and 1
print(R) # [18, 22, 40, 50]
del R[2:] # to delete elements from index no 2 to last
print(R) # [18, 22]
R.extend(['A','B','C','D','E','F'])
print(R) # [18, 22, 'A', 'B', 'C', 'D', 'E', 'F']
del R[::3] # to delete every 3rd occurrence elements of the list
print(R) # [22, 'A', 'C', 'D', 'F']
del R # to delete entire list
print(R) # NameError: name 'R' is not defined
Unpacking List :
Creating a list from a set of values is called packing and its reverse, i.e.
creating individual values from a List’s element is called unpacking.
Syntax: var1,var2,var3……=list
Note: The number of variables in the left side of assignment must match the
number of elements in the List.
e.g.
L=[3,4,5] # packing
a,b,c=L # Unpacking
print(a,"-",b,"-",c)
d=a+b
print(d)
Output
3-4-5
7
M=[2,4,5]
N=[10,20]
print(M,N)
K=M # shallow copy
L=list(N) # deep copy Output:
K.extend([100,200]) [2, 4, 5] [10, 20]
L.extend([200,400]) [2, 4, 5, 100, 200] [10, 20]
print(M,N) [2, 4, 5, 100, 200, 10, 20]
R=M+N [2, [10, 20], 4, 5, 100, 200]
print(R)
M.insert(1,N)
print(M)
H=[2,3,5,6,8,4,5,3,10] Output:
print(H) [2, 3, 5, 6, 8, 4, 5, 3, 10]
print(H.pop(3))
print(H.pop(-5))
6
print(H.pop()) 8
print(H) 10
print(H.remove(3))
[2, 3, 5, 4, 5, 3]
print(H)
print(H.count(5),H.count(100)) None
H.sort() [2, 5, 4, 5, 3]
print(H) 20
H.sort(reverse=True)
print(H)
[2, 3, 4, 5, 5]
H.clear() [5, 5, 4, 3, 2]
print(H) []
Tuples: It is a sequence of immutable objects.
It is just like a list.
Difference between a tuple and a list is that the tuple cannot
be changed like a list.
List uses square bracket whereas tuple use parentheses.
Note: T=(1) # here python considers it a value not the element of tuple
Unpacking tuple :
Creating a tuple from a set of values is called packing and its reverse, i.e.
creating individual values from a tuple’s element is called unpacking.
Syntax: var1,var2,var3……=tuple
Note: The number of variables in the left side of assignment must match the
number of elements in the tuple.
e.g.
t=(3,4,5) # packing
a,b,c=t # Unpacking
print(a,"-",b,"-",c)
d=a+b
print(d)
Output
3-4-5
7
#Get elements from 3 to -4
Accessing Values from Tuples/tuple slicing:
Use the square brackets for slicing along with the index or
indices to obtain the value available at that index.
e.g.
tup1 = (“Comp sc", “Info practices", 2020, 2021)
tup2 = (5,11,22,44,9,66)
print (“Res:", tup1[0])
print (“Res :", tup2[1:5])
Output
Res: Comp sc
Res:(11, 22, 44, 9)
e.g.:
# examples based on tuples (A immutable list)
T=tuple("computer")
R=eval(input("Enter the tuple-")) #(4,5,6,"abc")
S=tuple(input("Enter the tuple-")) #12345
print(T)
print(R) pro2017
print(S)
Output:
Enter the tuple-(4,5,6,"abc")
Enter the tuple-12345
('c', 'o', 'm', 'p', 'u', 't', 'e', 'r')
(4, 5, 6, 'abc')
('1', '2', '3', '4', '5')
e.g.:
t=eval(input("enter elements-"))
print(t)
print(t[0]+t[1])
enter elements-(2,4,5,6,"sdsds")
(2, 4, 5, 6, 'sdsds')
6
Updating Tuples :
Tuples are immutable, that’s why we can’t change the content
of tuple.
An alternate way is to take contents of existing tuple and
create another tuple with these contents as well as new
content.
e.g.
tup1 = (1, 2)
tup2 = ('a', 'b’)
tup3 = tup1 + tup2
print (tup3)
Output (1, 2, 'a', 'b')
Delete Tuple Elements:
Direct deletion of tuple element is not possible but
shifting of required content after discard of unwanted
content to another tuple.
e.g.
T = (1, 2,3,4,5,6)
T1 = T[0:3] + T[4:]
print (T1)
Output (1,2,3,5,6)
NOTE : Entire tuple can be deleted using del statement.
e.g. del T
2
OR r=sum(X)
sum(x)
*max of a tuple elements.
x = (1,4,6)
m= max(x)
print(‘Maximum value in tuple=', m)
L=[4,3,6,1,7]
L.sort()
[1, 3, 4, 6, 7]
print(L)
[7, 6, 4, 3, 1]
L.sort(reverse=True)
print(L)
More example on Tuple:
K=(2,5,6,2)
for a in K: # Traversing
print(a,end=" ") #2 5 6 2
# creating tuple from string
T=tuple("hello")
print(T) # ('h', 'e', 'l', 'l', 'o')
# creating tuple from List
T=tuple([5,6,7])
print(T) # (5, 6, 7)
# creating tuple from dictionary
T=tuple({1:"A",2:"B"})
print(T) # (1, 2)