List Problems
List Problems
List1=[10,20,"ABC",[100,200,300]]
List2=["Apple","Orange","Grape"]
List3=[(1,2),[3,4],"56",78,9.0]
List4=[10,20,30,40,50,60,70,80,90]
print("List1",List1)
print("List2",List2)
print("List3",List3)
print("List4",List4)
print("List1[2]",List1[2])
print("List1[3]",List1[3])
print("List1[3][2]",List1[3][2])
print("List3[0]",List3[0])
print("List3[0][0]",List3[0][0])
print("List4[:]",List4[:])
print("List4[1:]",List4[1:])
print("List4[1:-1]",List4[1:-1])
print("List4[1:5]",List4[1:5])
print("List4[1:5:-1]",List4[1:5:-1])
print("List4[5:1:-1]",List4[5:1:-1])
print("List4[2:-5]",List4[2:-5])
print("List4[6:2]",List4[6:2])
print("List4[6:2:-1]",List4[6:2:-1])
print("List4[-8:-2]",List4[-8:-2])
print("List4[-2:-8]",List4[-2:-8])
print("List4[-2:-8:-2]",List4[-2:-8:-2])
print("List4[::2]",List4[::2])
print("List4[::-1]",List4[::-1])
print("List4[2::-2]",List4[2::-2])
print("List4[::]",List4[::])
print("List4[2:3]",List4[2:3])
print("List4[2:2]",List4[2:2])
print("Length of List3",len(List3))
print("Append Python to List 3")
List3.append("Python")
print("List3",List3)
print("Reversing List3")
List3.reverse()
print("List3",List3)
print("Pop an element from List3, List3.pop()",List3.pop())
print("Pop 2nd Element from List3, List3.pop(2)",List3.pop(2))
print("Maximum in List4",max(List4))
print("Minimum in List4",min(List4))
print("Copy List4 to List5, List5=list4.copy() - CLONING")
List5=List4.copy()
print("List4",List4)
print("List5",List5)
print("Update value of list4[3]=100")
List4[3]=100
print("List4",List4)
print("List5",List5)
print("Aliasing List6=List1")
List6=List1
print("Update value of list1[1]=Programming")
List1[1]="Programming"
print("List1",List1)
print("List6",List6)
print("Concatenation List 1+List2\n",List1+List2)
OUTPUT
List1 [10, 20, 'ABC', [100, 200, 300]]
List2 ['Apple', 'Orange', 'Grape']
List3 [(1, 2), [3, 4], '56', 78, 9.0]
List4 [10, 20, 30, 40, 50, 60, 70, 80, 90]
List1[2] ABC
List1[3] [100, 200, 300]
List1[3][2] 300
List3[0] (1, 2)
List3[0][0] 1
List4[:] [10, 20, 30, 40, 50, 60, 70, 80, 90]
List4[1:] [20, 30, 40, 50, 60, 70, 80, 90]
List4[1:-1] [20, 30, 40, 50, 60, 70, 80]
List4[1:5] [20, 30, 40, 50]
List4[1:5:-1] []
List4[5:1:-1] [60, 50, 40, 30]
List4[2:-5] [30, 40]
List4[6:2] []
List4[6:2:-1] [70, 60, 50, 40]
List4[-8:-2] [20, 30, 40, 50, 60, 70]
List4[-2:-8] []
List4[-2:-8:-2] [80, 60, 40]
List4[::2] [10, 30, 50, 70, 90]
List4[::-1] [90, 80, 70, 60, 50, 40, 30, 20, 10]
List4[2::-2] [30, 10]
List4[::] [10, 20, 30, 40, 50, 60, 70, 80, 90]
List4[2:3] [30]
List4[2:2] []
Length of List3 5
Append Python to List 3
List3 [(1, 2), [3, 4], '56', 78, 9.0, 'Python']
Reversing List3
List3 ['Python', 9.0, 78, '56', [3, 4], (1, 2)]
Pop an element from List3, List3.pop() (1, 2)
Pop 2nd Element from List3, List3.pop(2) 78
Maximum in List4 90
Minimum in List4 10
Copy List4 to List5, List5=list4.copy() - CLONING
List4 [10, 20, 30, 40, 50, 60, 70, 80, 90]
List5 [10, 20, 30, 40, 50, 60, 70, 80, 90]
Update value of list4[3]=100
List4 [10, 20, 30, 100, 50, 60, 70, 80, 90]
List5 [10, 20, 30, 40, 50, 60, 70, 80, 90]
Aliasing List6=List1
Update value of list1[1]=Programming
List1 [10, 'Programming', 'ABC', [100, 200, 300]]
List6 [10, 'Programming', 'ABC', [100, 200, 300]]
Concatenation List 1+List2
[10, 'Programming', 'ABC', [100, 200, 300], 'Apple', 'Orange', 'Grape']
2. One of the following 10 statements generates an error. Which one? (Your answer
should be a number between 1 and 10.)
x = ["sun",[17],2,"king",[3,4]] # Statement 1
y = x[0:8] # Statement 2
z=x # Statement 3
w=y # Statement 4
z[0] = 0 # Statement 5
y[0] = y[0][0:3] + 'k' # Statement 6
y[1][1:3] = [5,8] # Statement 7
x[0] = x[0][1:3] # Statement 8
w[4][0] = 1000 # Statement 9
a = (x[4][1] == 4) # Statement 10
Answer: Statement 8
3. Consider the following lines of Python code.
x = [589,'big',397,'bash']
y = x[2:]
u=x
w=y
w = w[0:]
w[0] = 357
x[2:3] = [487]
def mystery(l):
l[0:2] = l[3:5]
return()
list1 = [34,17,12,88,53,97,62]
mystery(list1)
Answer: [88, 53, 12, 88, 53, 97, 62]