List
List
1. Write a program to read input from user and store it inside list.
for i in range(n):
d = int(input("Enter the value:"))
a.append(d)
print(a)
a = [10,20,30,40,50]
for i in a:
print(i)
a = [10,20,30,40,50]
for i in range(len(a)):
print(a[i])
3. Write a program to read n values from user and print sum of those values
List 1
a.append(d)
sum = 0
for i in a:
sum = sum+i
print(a)
print("Sum of elements in a list:",sum)
4. Write a program to read n names from user and print the highest length
name
max = 0
index = 0
for i in range(len(a)):
if len(a[i]) > max:
max = len(a[i])
index = i
longestName = max(a,key=len)
List 2
5. Write a program to read n integer values from user and print in that how
many numbers are prime
def isPrime(n):
if n==0 or n==1:
return False
for i in range(size):
d = int(input("Enter the value:"))
l.append(d)
count = 0
for i in l:
if isPrime(i):
count = count+1
for i in range(size):
d = int(input("Enter the value:"))
l.append(d)
print(l)
big = l[0]
List 3
for i in l:
if i>big:
big = i
for i in range(size):
d = int(input("Enter the value:"))
l.append(d)
print(l)
l = [10,20,30,40,50,60]
l2=[]
for i in range(len(l)-1,-1,-1):
l2.append(l[i])
print(l)
print(l2)
l = [10,20,30,40,50,60]
print(l)
i=0
j=len(l)-1
while i<j:
temp = l[i]
List 4
l[i] = l[j]
l[j] = temp
i = i+1
j = j-1
print(l)
l = [10,20,30,40,50,60]
l = l[::-1]
print(l)
l1 = [10,20,30,40,50,60]
l2 = [80,90,100]
print(l1+l2)
l1 = [10,20,30,40,50,60]
l2 = [80,90,100]
l3 = []
for i in l1:
l3.append(i)
for i in l2:
l3.append(i)
print(l3)
9. Define a method to return the difference between the biggest and smallest
element in the given array
l1 = [10,20,30,40,50,60]
List 5
big = l1[0]
small = l1[0]
for i in l1:
if i>big:
big = i
if i<small:
small = i
print("Biggest element:",big)
print("Smallest element:",small)
print("Difference:",big-small)
l1 = [10,20,30,40,50,60]
big = max(l1)
small = min(l1)
print(big-small)
def find_frequency(arr):
frequency = [] # List to store (element, count) pairs
visited = [] # List to track elements that are already
for i in arr:
if i not in visited:
count = 0
for j in arr:
if i == j:
count += 1
frequency.append((i, count)) # Store the eleme
visited.append(i) # Mark the element as visite
List 6
# Example usage
arr = [10, 20, 10, 30, 20, 40]
find_frequency(arr)
def find_frequency(arr):
frequency = Counter(arr)
l1 = [10,20,30,40,50,60]
l2 = [100,200,300]
r = []
i = 0
j = 0
List 7
j = j+1
print(l1)
print(l2)
print(r)
12. Define a method to merge two sorted array elements into single array in a
sorted array
l1 = [10,20,30,40,50,60]
l2 = [100,200,300]
r = []
i = 0
j = 0
print(l1)
print(l2)
print(r)
l1 = [10,20,30,40,50,60]
l2 = [100,300,200]
List 8
r = sorted(l1+l2)
print(r)
def find_frequency(arr):
frequency = [] # List to store (element, count) pairs
visited = [] # List to track elements that are already
for i in arr:
if i not in visited:
count = 0
for j in arr:
if i == j:
count += 1
frequency.append((i, count)) # Store the eleme
visited.append(i) # Mark the element as visite
highCount = 0
element = 0
for ele, count in frequency:
if count > highCount:
highCount = count
element = ele
return element
# Example usage
arr = [10, 20, 10, 30, 20, 40,40,40,40]
print(find_frequency(arr))
List 9
def find_frequency(arr):
frequency = Counter(arr)
highCount = 0
element = 0
return element
for i in arr:
if i == element:
count += 1
return count
List 10
def remove_duplicates(arr):
uniqueElements = []
for i in arr:
if i not in uniqueElements:
uniqueElements.append(i) # Add it if it's not
return uniqueElements
# Example usage
arr = [10, 20, 10, 30, 20, 40, 20]
print("Array after removing duplicates:", remove_duplicates
def remove_duplicates(arr):
return list(set(arr))
16. Write a program to delete repeated elemetns in array without taking extra
array
def remove_duplicates(arr):
n = len(arr) # Length of the original array
i = 0 # Pointer for the current position in the array
List 11
else:
j += 1 # Move to the next element
i += 1 # Move to the next unique element
# Example usage
arr = [10, 20, 10, 30, 20, 40, 20]
print("Array after removing duplicates:", remove_duplicates
return common
# Example usage
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
print("Common elements:", common_elements(list1, list2))
# Example usage
list1 = [1, 2, 3, 4, 5]
List 12
list2 = [4, 5, 6, 7, 8]
print("Common elements:", common_elements(list1, list2))
19. Define a method to accept user entered number and return it in words.
def numToWord(n,st):
if n==0:
return
x = ["","one","two","three","four","five",'six','seven
y = ['','','twenty','thrity','forty','fifty','sixty','s
if n < 20:
print(x[n],end=" ")
else :
print(y[n//10]+x[n%10],end=" ")
print(st,end=" ")
numToWord(n//10000000,"crore")
numToWord(n//100000%100,"lakh")
numToWord(n//1000%100,"thousand")
numToWord(n//100%10,"hundred")
numToWord(n%100,"")
def shift_right(l):
if len(l) == 0:
return lst # Handle empty list case
List 13
# Shift all other elements to the right
for i in range(len(l) - 1, 0, -1):
l[i] = l[i - 1]
l[0] = temp # Set the first element to the previous la
return l
# Example usage
lst = [1, 2, 3, 4, 5]
shifted_lst = shift_right(lst)
print(shifted_lst)
def shift_left(lst):
if len(lst) == 0:
return lst # Handle empty list case
return lst
# Example usage
lst = [1, 2, 3, 4, 5]
shifted_lst = shift_left(lst)
print(shifted_lst)
List 14