Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Reg No: URK22CS1007

EX NO:4
List in Python
29.1.23

1. Write a Python program to find the second smallest and second largest

number in a list

Aim:
To write a Python program to find the second smallest and Second largest number in a list

Algorithm:
Step 1: Start the program.

Step 2: sort the list using sort() method.

Step 3: find the length of the list using len() method and store it in ‘n’.

Step 4: the second largest element will be in the index (n-2),the second smallest element will be in the
index 1.

Step 5: print the result.

Step 6: Stop the program.

Program:
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]

list.sort()

n=len(list)

print("The second largest element is:",list[n-2])

print("The second smallest element is:",list[1])

Output:
2. Write a Python program to get the frequency of the elements in a list

Aim:

To write a Python program to get the frequency of the elements in a list

Algorithm:

Step 1: Start the program.

Step 2: Declare an empty list n.

Step 3: Use for loop to print the elements along with its frequency of occurrence using count()
methods

Step 4: As we traverse through the for loop, append each element to the empty list n. and for every
iteration compare current elements with n to avoid repetition.

Step 5: Stop the program.

Program:

List= [1, 2, 8, 3, 2, 2, 2, 5, 1]

n=[]

for x in List:

if x not in n:

print(x,"|",List.count(x))

n.append(x)

Output:
3. Write a Python program to remove duplicate elements in a list.
Aim:
To write a Python program to remove duplicate elements in a list.

Algorithm:
Step 1: Start the program.

Step 2: Store the elements in a list ‘a’.

Step 3: Convert the list into a set ‘x’.

Step 4: print the set ‘x’, in python set doesn’t recognize duplicate elements. Only unique elements will
be printed

Step 5: Stop the program.

Program:
a = [10,20,30,20,10,50,60,40,80,50,40]

x=set(a)

print(x)

Output:

4. Write a python program to generate 10 random numbers from 1 to 50

and insert them into a list.

Aim:
To Write a python program to generate 10 random numbers from 1 to 50 and insert them into a list.

Algorithm:
Step 1: Start the program.

Step 2: Import random module.

Step 3: read the number of elements from the user and store it in variable n.

Step 4: create an empty list named random_list

Step 5: use for loop to generate random numbers using randint() or randrange() functions of the
random module.
Step 6: insert these random numbers to the list random_list

Step 7: Print the list

Step 8: Stop the program

Program:
import random

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

random_list=[]

pos=0

for x in range(1,n+1):

rand=random.randint(1,51)

random_list.insert(pos,rand)

pos+=1

print("Randomized list=",random_list)

Output:

5. Write a python program to print each and every element in reverse


order.

Aim:
To write a python program to print each and every element in reverse order.

Algorithm:
Step 1: Start the program.

Step 2: Declare the list.

Step 3: print the list with a negative step value.

Step 4: Stop the program.


Program:

list = [4, 5, 6, 7, 8, 9]

print("Output:",list[::-1])

Output:

Result:
These programs are executed successfully and the result is displayed on the screen

You might also like