Python Sort List. The Sort Method Is Used To Sort The Elements in A Specific Order I.E
Python Sort List. The Sort Method Is Used To Sort The Elements in A Specific Order I.E
The concepts in Python Advanced list includes Python Sort Method, Sorted function, Python
Reverse List, Python Index Method, Copying a List, Python Join Function, Sum Function,
Removing duplicates from the List, Python List Comprehension, etc.
Python Sort List. The sort() method is used to sort the elements in a specific order i.e.
Ascending or Descending. If you want to sort the elements in Ascending order, then you
can use the following syntax.
list.sort()
If you want to sort the elements in Descending order, then you can use the following syntax.
list.sort(reverse=True)
Example:
Input:
Students.sort()
print(Students)
Output:
Input:
print(Students)
Output:
Thus sort() method is used to arrange a list in either Ascending or Descending order. One
more important thing to remember here is that sort() method changes the order of the list
permanently. If you want to change the order of the list temporarily, then you need to use
sorted() function.
Sorted function. In order to maintain the original order of the list that is present in sorted
order, you can use the sorted() function. The sorted() function allows you to display your
list in a particular order, without affecting the actual order of the list.
Example:
Input:
print(sorted(Students))
print(Students)
Output:
As you can see from the output, the original order of the list remains intact.
You can also print the list in a reverse order using the sorted function in the following
manner:
print(sorted(Students))
print(Students)
Output:
Python Reverse List. In order to reverse the original order of a list, you can use the reverse()
method. The reverse() method is used to reverse the sequence of the list and not to arrange
it in a sorted order like the sort() method.
Example:
Input:
Students.reverse()
print(Students)
Output:
reverse() method reverses the sequence of the list permanently. Hence in order to get back
to the original sequence of the list apply the reverse() method again to the same list.
Example:
Input:
Students = ['Harsh','Andrew','Danny','Ritesh','Meena']
print(Students.index('Danny'))
Output:
If you search for an element which is not present in the list, then You will get an error.
Input:
Students = ['Harsh','Andrew','Danny','Ritesh','Meena']
print(Students.index('Vammy'))
Output:
For Example, imagine we have a list of our favorite foods and we want to make a separate
list of foods that a friend likes. This friend likes everything in our list so far, so we can create
that list by copying ours.
Input:
friend_foods = my_foods[:]
print(my_foods)
print(friend_foods)
Output:
Input:
my_foods_csv=",".join(my_foods)
Output:
In the above example, you can see that we have the my_foods list which we have appended
in a string variable named as my_foods_csv using the join function.
Python Sum List function. Python provides an in-built function called sum() which sums up
the numbers in the list.
Example:
Input:
numbers = [4,6,8,9,3,7,2]
Sum = sum(numbers)
print(Sum)
39
In the above example, we have taken a list of numbers and using the sum function we have
added all the numbers.
Python Remove Duplicates from the List. As you know, a list can contain duplicates. But in
case, if you want to remove the duplicate from a list, how can you do it?
The simple way is to convert the list to the dictionary using the list item as keys. This will
automatically remove any duplicates as dictionaries cannot have duplicate keys and all the
items in the list will tend to appear in correct order.
Example:
Input:
numbers = [4,6,8,9,3,7,2]
Sum = sum(numbers)
print(Sum)
Output:
39
In the above example we have a list with duplicate elements and from that, we have created
a dictionary, Again we have created a list out of that dictionary, and finally, we get a list
without any duplicates.
Creating a unique list from the list having duplicate elements is another way to remove
duplicates from a list.
mylist = [4, 5, 6, 5, 4]
uniqueList = []
uniqueList.append(elem)
print(uniqueList)
Output:
[4, 5, 6]
In the above example, we have created a unique list and then appended the unique items
from the list to another list.
List Comprehension. If you want to create a list which contains the squares of numbers
from 1 to 10, then you can do it using for-loop.
Example:
Input:
squares = []
square = value**2
squares.append(square)
print(squares)
The above process takes 3 to 4 lines of code. But using List comprehension it can be
accomplished in just one line of code.
Input:
print(squares)
Output:
In the above example, we begin with a descriptive name for the list i.e. squares. Next, we
open a set of square brackets and define the expression for the values that we want to store
in the new list. In this example, the expression value that raises the value to the second
power is **2.
Then, write a for loop to generate the numbers you want to feed into the expression and
close the square brackets. The for loop in this example is for the value in the range(1,11),
which feeds the values 1 through 10 into the expression value**2.
Reference: Softwaretestinghelp.com