- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Sort by Rear Character in Strings List
When it is required to sort the list by rear character, a method is defined that uses negative indexing to return the result.
Below is a demonstration of the same −
def get_rear_position(element): return element[-1] my_list = ['python', 'is', 'fun', 'to', 'learn'] print("The list is : ") print(my_list) my_list.sort(key = get_rear_position) print("The result is : ") print(my_list)
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ['python', 'fun', 'learn', 'to', 'is']
A method is defined that takes element of the list as a parameter and returns the last element as output using negative indexing.
A list is defined and displayed on the console.
The list is now sorted using key as the previously defined method.
This is the output that is displayed on the console.
- Python – Sort String list by K character frequency
- Python – Sort Strings by Case difference
- Python – Sort given list of strings by numeric part of string
- Python program to Sort Strings by Punctuation count
- Python program to sort strings by substring range
- Python How to sort a list of strings
- Python – Concatenate Rear elements in Tuple List
- Sort by character length in MySQL
- How to sort a list of strings in Python?
- Python program to Sort a List of Strings by the Number of Unique Characters
- Python – Sort given list of strings by part the numeric part of string
- Python Front and rear range deletion in a list?
- Python – Sort a List by Factor count
- Rear element extraction from list of tuples records in Python
- Sort list of tuples by specific ordering in Python