List in Python :
The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.
One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.
Given a list, the task is to sort the list according to the length of the elements in the given list
- Python Program to Print Largest Even and Largest Odd Number in a List
- Python Program to Find the Second Largest Number in a List
- Python Program to Calculate the Average of Numbers in a Given List
Example:
Input:
given list = ["hello", "this", "is", "BTechGeeks", "online", "platform", "for", "Python", "and", "many", "Programming", "languages"]
Output:
printing the given list before sorting according to length : ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages'] printing the given list after sorting according to length : ['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']
Python Program to Sort a List According to the Length of the Elements
There are several ways to sort the given list according to the length of the elements some of them are:
- Using sort function ( Static Input )
- Using sort function ( User Input )
- Using sorted function ( Static Input )
- Using sorted function ( User Input )
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1:Using sort function ( Static Input )
Approach:
- Give the list input as static
- Then use sort() function to sort the given list
- Pass key=len as parameters to the sort function as we have to sort the list according to the length of elements.
- print the list
Below is the implementation:
def lengthSort(given_list): # sorting the given list by length of elements of the given list given_list.sort(key=len) # return the list return given_list # given list given_list = ["hello", "this", "is", "BTechGeeks", "online", "platform", "for", "Python", "and", "many", "Programming", "languages"] # printing the given list before sorting according to length print("printing the given list before sorting according to length : ") print(given_list) # passing the given_list to lengthSort function to sort # the list according to the length of the elements of the given list # printing the given list before sorting according to length print("printing the given list after sorting according to length : ") print(lengthSort(given_list))
Output:
printing the given list before sorting according to length : ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages'] printing the given list after sorting according to length : ['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']
Method #2:Using sort function ( User Input )
Approach:
- Scan the list as string and convert it to list of strings using split() function.
- Then use sort() function to sort the given list
- Pass key=len as parameters to the sort function as we have to sort the list according to the length of elements.
- print the list
Below is the implementation:
def lengthSort(given_list): # sorting the given list by length of elements of the given list given_list.sort(key=len) # return the list return given_list # given list given_list = list( input("Enter the elements of the given list separated by spaces = ").split()) # printing the given list before sorting according to length print("printing the given list before sorting according to length : ") print(given_list) # passing the given_list to lengthSort function to sort # the list according to the length of the elements of the given list # printing the given list before sorting according to length print("printing the given list after sorting according to length : ") print(lengthSort(given_list))
Output:
Enter the elements of the given list separated by spaces =hello this is BTechGeeks online platform for Python and many Programming languages printing the given list before sorting according to length : ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages'] printing the given list after sorting according to length : ['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']
Method #3:Using sorted function ( Static Input )
Approach:
- Give the list input as static
- Then use sort() function to sort the given list
- Pass given list and key=len as parameters to the sorted() function as we have to sort the list according to the length of elements.
- print the list
Below is the implementation:
def lengthSort(given_list): # sorting the given list by length of elements of the given list given_list = sorted(given_list, key=len) # return the list return given_list # given list given_list = ["hello", "this", "is", "BTechGeeks", "online", "platform", "for", "Python", "and", "many", "Programming", "languages"] # printing the given list before sorting according to length print("printing the given list before sorting according to length : ") print(given_list) # passing the given_list to lengthSort function to sort # the list according to the length of the elements of the given list # printing the given list before sorting according to length print("printing the given list after sorting according to length : ") print(lengthSort(given_list))
Output:
printing the given list before sorting according to length : ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages'] printing the given list after sorting according to length : ['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']
Method #4:Using sorted function ( User Input )
Approach:
- Scan the list as string and convert it to list of strings using split() function.
- Then use sorted() function to sort the given list
- Pass given list and key=len as parameters to the sorted() function as we have to sort the list according to the length of elements.
- print the list
Below is the implementation:
def lengthSort(given_list): # sorting the given list by length of elements of the given list given_list = sorted(given_list, key=len) # return the list return given_list # given list given_list = list( input("Enter the elements of the given list separated by spaces").split()) # printing the given list before sorting according to length print("printing the given list before sorting according to length : ") print(given_list) # passing the given_list to lengthSort function to sort # the list according to the length of the elements of the given list # printing the given list before sorting according to length print("printing the given list after sorting according to length : ") print(lengthSort(given_list))
Output:
Enter the elements of the given list separated by spaces =hello this is BTechGeeks online platform for Python and many Programming languages printing the given list before sorting according to length : ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages'] printing the given list after sorting according to length : ['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']
Related Programs: