24. Problem to sort a list using the sort and sorted method.

In this program, we will take a user input as a list. Sort the given list using the sort and sorted method with the help of the below-given steps.

Table of Contents

Sort and sorted method:

Method 1 : Using Sort()

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the output.
				
					#Input list
list1 = [23,11,78,45,33]

#Using sort
list1.sort()
print("By sort function: ",list1)
				
			

Output :

				
					By sort function:  [11, 23, 33, 45, 78]

				
			

Method 2 : Using sorted()

Steps to solve the program
  1. Take a list as input,
  2. Sort the list using sorted().
  3. Print the output.
				
					#Input list
list1 = [23,11,78,45,33]

#using sorted
print("By sorted method: ",sorted(list1))
				
			

Output :

				
					By sorted method:  [11, 23, 33, 45, 78]
				
			

Related Articles

Add two lists using extend method

Remove data from the list using pop method

Leave a Comment