58. Problem to select random numbers from the list.

In this Python list program, we will take a user input as a list and select random numbers from the list with the help of the below-given steps.

Select random numbers from the list:

Steps to solve the program
  1. Take a list as input.
  2. Create an empty list and a variable.
  3. Import a random library to select random numbers from the list.
  4. Add those randomly generated numbers to the empty list.
  5. Print the list two see the output.
				
					#Input list
list1 = [1, 4, 5, 7, 3, 2, 9]

#Creating an empty list
list2 = []
count=0

#Importing random library
import random

while count<4:
    list2.append(random.choice(list1))
    count += 1

#Printing output
print(list2)
				
			

Output :

				
					[7, 7, 4, 3]
				
			

Related Articles

Insert items in the list at a specific location

Create a 3*3 grid with numbers

Leave a Comment