0% found this document useful (0 votes)
17 views

Python (List & Tuple)

The document discusses various operations that can be performed on lists in Python. It covers slicing lists, updating/manipulating list elements, making true copies of lists, adding/deleting list elements using methods like append(), insert(), remove(), pop(), clear(), as well as sorting, reversing lists. It also demonstrates using functions like count(), min(), max() on lists and deleting list elements or slices using the del statement.

Uploaded by

krishssinghsoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Python (List & Tuple)

The document discusses various operations that can be performed on lists in Python. It covers slicing lists, updating/manipulating list elements, making true copies of lists, adding/deleting list elements using methods like append(), insert(), remove(), pop(), clear(), as well as sorting, reversing lists. It also demonstrates using functions like count(), min(), max() on lists and deleting list elements or slices using the del statement.

Uploaded by

krishssinghsoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

# Slicing the lists seq=L[start: stop: step]

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

print (LI[::3]) # to display every 3rd element of the sequence


print (LI[::-2]) # to display every 2nd element of the sequence from the back
print (LI[::5]) # to display every 5th element of the sequence
print (LI[::-4]) # to display every 4th element of the sequence from the back
OUTPUT:
-9 -8 -7 -6 -5 -4 -3 -2 -1 [10, 6, 35, 100, 45]
[10, 35, 45]
10 4 6 25 35 8 100 20 45 [10, 25, 100]
0 1 2 3 4 5 6 7 8 [45, 100, 35, 6, 10]
[10, 8]
[45, 35, 10]
Updating / Manipulating elements of LIST:
We can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator.
e.g.1) e.g.2)
LI= [1,2,3,4,5,6,7,8,9,10] LI= [1,2,3,4,5,6,7,8,9,10]
print (LI) print (LI)
LI[1:2] = 100,200 # for index no 1 LI[1:5] = 100,200 # from index no 1 to 4
print(LI) print(LI)
OUTPUT: OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 100, 200, 3, 4, 5, 6, 7, 8, 9, 10] [1, 100, 200, 6, 7, 8, 9, 10]
Updating / Manipulating elements of LIST:
We can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator.
e.g.3) e.g.4)
LI= [1,2,3,4,5,6,7,8,9,10] LI= [1,2,3,4,5,6,7,8,9,10]
print (LI) print (LI)
LI[3:] = 100,200 #from index no 3 to last LI[:5] = 100,200 # from index no 0 to 4
print(LI) print(LI)
OUTPUT: OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 100, 200] [100, 200, 6, 7, 8, 9, 10]
Updating / Manipulating elements of LIST:
We can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator.
e.g.5) e.g.6)
LI= [1,2,3,4,5,6,7,8,9,10] LI= [1,2,3,4,5,6,7,8,9,10]
print (LI) print (LI)
LI[:] = 100,200 # to replace all LI[::2] = 'A','B','C','D',’E’ #slices of 2
print(LI) print(LI)
OUTPUT: OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[100, 200] ['A', 2, 'B', 4, 'C', 6, 'D', 8, 'E', 10]
Updating / Manipulating elements of LIST:
We can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator.
e.g.7) e.g.8)
LI= [5,10,15,20,25,30,35,40,45,50] LI= [5,10,15,20,25,30,35,40,45,50]
print (LI) print (LI)
LI[::-2] = 11,22,33,44,55 # from back LI[0:10:3] = 'A','B','C','D’#slices of 4
print(LI) print(LI)
OUTPUT: OUTPUT:
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50] [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
[5, 55, 15, 44, 25, 33, 35, 22, 45, 11] ['A', 10, 15, 'B', 25, 30, 'C', 40, 45, 'D']
Delete an item from a LIST
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’]

G=K # Does not copy the list


So, if you make changes in one list, the other list will also report those changes
because these two list names are labels referring to same list because ‘=’
copied the reference not the actual list.

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’]

G=K # Does not copy the list


So, if you make changes in one list, the other list will also report
those changes because these two list names are labels referring to
same list because ‘-’ copied the reference not the actual list.
(1st occurrence of the given item from the list)
Add Item to A List append() method is used to add an Item to
a List.

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])

Add Two Lists


e.g. list = [1,2]
list2 = [3,4,5,6]
list3 = list + list2
print(list3)

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.

L=[1,2,3,4,5] Mutable Elements of list can be changed


T=(1,2,3,4,5) Immutable Elements of tuple can not be changed

 A tuple is enclosed in parentheses () for creation and each


item is separated by a comma.
 NOTE:- Indexing of tuple is just similar to indexing of list
(5,) create tuple with one number
Special:
 We can also create an empty tuple as:
T=tuple() or T=()
 To construct a tuple with one element just add a
comma after the single element as shown below
T=3, or T=(3,)

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)

OUTPUT: Maximum value in tuple =6


*mean/average of a tuple elements.
x = (1,4,6)
r= sum(x)/len(x)
print(‘Mean of tuple is: ', r)

OUTPUT: Mean of tuple is: 3.66


test(tuple).py
k=(3,7,8,2,67,9)
k=sorted(k) [2, 3, 7, 8, 9, 67]
print(k) [67, 9, 8, 7, 3, 2]
k=sorted(k,reverse=True)
print(k)

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)

You might also like