• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Tutorials
      • Node.js Tutorial
      • Express.js Tutorial
      • Python Tutorial
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements

Sorting Python Lists| sort() method in Python Lists

  • October 16, 2022
  • CODE OF GEEKS
  • 0
Python Lists : In this Unit…
  • Python Lists Basics
  • index() method in list
  • append() method in list
  • insert() method in list
  • copy() method in list
  • remove() method in list
  • pop() method in list
  • sort() method in list
  • reverse() method in list
  • clear() method in list
  • Nested Lists in Python
  • List Comprehensions in Python


sort() method in Python Lists can be used to sort all the elements of a Python List in the ascending order.

The sort() method in Python Lists returns ‘None‘ as a value, however, it modifies the original array.

Syntax

list.sort()
l = [3, 1, 5, 2, 6]
print(l.sort()) # returns None
print(l) # returns sorted list

O/P

None
[1, 2, 3, 5, 6]

If we want to sort the elements of the list into descending order, then we can write ‘reverse=True‘ in the sort method as :

l = [3, 1, 5, 2, 6]
l.sort(reverse = True) # returns None
print(l) # returns sorted list

O/P

[6, 5, 3, 2, 1]

If you don’t want to use sort() method to sort Python list, then, you can do it using any of the pre-defined sorting techniques like bubble sort, selection sort, insertion sort, merge sort, quick sort etc.

Extra Gyan !

We can also use sorted() method to sort the Python List.

The sorted() function returns a sorted list of the specified iterable object.

You can specify ascending or descending order.

l = [3, 1, 5, 2, 6]
print(sorted(l)) # returns sorted list
print(sorted(l, reverse=True)) # returns sorted list

O/P

[1, 2, 3, 5, 6]
[6, 5, 3, 2, 1]

That’s all here.





  • Previous Reversing Python Lists| reverse() method in Python Lists
  • Next Popping element from Python Lists| pop() method in Python Lists

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Advertise with us

Python Tutorial

NodeJS Tutorial

Express.JS Tutorial

Study Materials : Company Wise

Verbal Ability for Placements

CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions
Refund Policy
Contact us

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Tutorials

Python
Node.js
Express.js
Golang

Recent Posts

Strings in Go and its basic operations
  • August 11, 2023
Introduction to Functions in Go programming
  • August 11, 2023

Copyright @ CODE OF GEEKS. All Rights Reserved.