0% found this document useful (0 votes)
3 views14 pages

List

Uploaded by

hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

List

Uploaded by

hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

List

Slicing in Python is a powerful feature that allows you to extract a portion of a


sequence (such as a list, tuple, or string) by specifying a start index, stop
index, and step.

1. Write a program to read input from user and store it inside list.

n = int(input("Enter the size of list:"))


a = list()

for i in range(n):
d = int(input("Enter the value:"))
a.append(d)

print(a)

2. Write a program to traverse a list

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

n = int(input("Enter the size of list:"))


a = list()
for i in range(n):
d = int(input("Enter the value:"))

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

n = int(input("Enter the size of list:"))


a = list()
for i in range(n):
d = input("Enter the value:")
a.append(d)

max = 0
index = 0

for i in range(len(a)):
if len(a[i]) > max:
max = len(a[i])
index = i

print("Highest length name:",a[index])

n = int(input("Enter the size of list:"))


a = list()
for i in range(n):
d = input("Enter the value:")
a.append(d)

longestName = max(a,key=len)

print("Highest length name:",longestName)

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(2, n//2+1):


if n%i == 0:
return False
return True

size = int(input("Enter the size of list:"))


l = []

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

print("The number of prime numbers in {} is {}".format(l,co

6. Write a program to define a method to return biggest element from array

size = int(input("Enter the size of list:"))


l = []

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

print("Biggest Element in the list:",big)

size = int(input("Enter the size of list:"))


l = []

for i in range(size):
d = int(input("Enter the value:"))
l.append(d)

print(l)

print("Biggest Element in the list:",max(l))

7. Define a method to reverse the array

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)

8. Define a method to combine two elements array into a single array

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)

10. Frequency of elements in array

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

# Print the frequency of each element


for element, count in frequency:
print(f"{element} occurs {count} time(s)")

List 6
# Example usage
arr = [10, 20, 10, 30, 20, 40]
find_frequency(arr)

from collections import Counter

def find_frequency(arr):
frequency = Counter(arr)

for element, count in frequency.items():


print(f"{element} occurs {count} time(s)")

arr = [10, 20, 10, 30, 20, 40]


find_frequency(arr)

11. Merge two elements into single array in zig zag

l1 = [10,20,30,40,50,60]
l2 = [100,200,300]
r = []

i = 0
j = 0

while i < len(l1) and j < len(l2):


r.append(l1[i])
r.append(l2[j])
i = i+1
j = j+1

while i < len(l1):


r.append(l1[i])
i = i+1

while j < len(l2):


r.append(l2[j])

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

while i < len(l1) and j < len(l2):


if l1[i] < l2[j]:
r.append(l1[i])
i = i+1
else:
r.append(l2[j])
j = j+1

while i < len(l1):


r.append(l1[i])
i = i+1

while j < len(l2):


r.append(l2[j])
j = j+1

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)

13. Define a method to return the highest occurence element

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

from collections import Counter

List 9
def find_frequency(arr):
frequency = Counter(arr)

highCount = 0
element = 0

for ele, count in frequency.items():


if count > highCount:
highCount = count
element = ele

return element

arr = [10, 20, 10, 30, 20, 40]


find_frequency(arr)

14. Define a method to return the frequency of the specified element

def elementFrequency(arr, element):


count = 0

for i in arr:
if i == element:
count += 1

return count

arr = [10, 20, 10, 30, 20, 40, 20]


element = 20
print(f"Frequency of {element}: {elementFrequency(arr, elem

arr = [10, 20, 10, 30, 20, 40, 20]


element = 20
print(f"Frequency of {element}: {arr.count(element)}")

15. Define a method to remove duplicate from array

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

arr = [10, 20, 10, 30, 20, 40, 20]


print("Array after removing duplicates:", remove_duplicates

16. Write a program to delete repeated elemetns in array without taking extra
array

# Program to delete repeated elements in an array without

def remove_duplicates(arr):
n = len(arr) # Length of the original array
i = 0 # Pointer for the current position in the array

# Traverse the array


while i < n:
j = i + 1 # Start comparing with the next element
while j < n:
if arr[i] == arr[j]: # If a duplicate is found
# Remove the duplicate by shifting elements
for k in range(j, n - 1):
arr[k] = arr[k + 1]
n -= 1 # Reduce the length of the array

List 11
else:
j += 1 # Move to the next element
i += 1 # Move to the next unique element

# Resize the array to the new length


return arr[:n] # Return the modified array

# Example usage
arr = [10, 20, 10, 30, 20, 40, 20]
print("Array after removing duplicates:", remove_duplicates

17. common element array

# Program to find common elements in two lists without usi

def common_elements(list1, list2):


common = [] # List to store common elements

for element in list1:


# Check if the element is in both lists
if element in list2 and element not in common:
common.append(element) # Add to common if not

return common

# Example usage
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
print("Common elements:", common_elements(list1, list2))

# Program to find common elements in two lists using in-bui

def common_elements(list1, list2):


# Use set intersection to find common elements
return list(set(list1) & set(list2))

# Example usage
list1 = [1, 2, 3, 4, 5]

List 12
list2 = [4, 5, 6, 7, 8]
print("Common elements:", common_elements(list1, list2))

18. nth biggest array

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=" ")

n = int(input("Enter the number:"))

numToWord(n//10000000,"crore")
numToWord(n//100000%100,"lakh")
numToWord(n//1000%100,"thousand")
numToWord(n//100%10,"hundred")
numToWord(n%100,"")

20. Right Shifting a list

def shift_right(l):
if len(l) == 0:
return lst # Handle empty list case

temp = lst[-1] # Store the last element

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)

21. Left Shifting in a list

def shift_left(lst):
if len(lst) == 0:
return lst # Handle empty list case

first_element = lst[0] # Store the first element

# Shift all other elements to the left


for i in range(1, len(lst)):
lst[i - 1] = lst[i]
lst[-1] = first_element # Set the last element to the

return lst

# Example usage
lst = [1, 2, 3, 4, 5]
shifted_lst = shift_left(lst)
print(shifted_lst)

List 14

You might also like