Write a Python program to sort each sublist of strings in a given list of lists

Python: Sort each sublist of strings in a given list of lists

Last update on November 23 2020 17:09:28 (UTC/GMT +8 hours)

Python: Sort each sublist of strings in a given list of lists using lambda

Last update on November 23 2020 17:09:34 (UTC/GMT +8 hours)

Python | Sort all sublists in given list of strings

Given a list of lists, the task is to sort each sublist in the given list of strings.

Example:

Input: lst = [['Machine', 'London', 'Canada', 'France'], ['Spain', 'Munich'], ['Australia', 'Mandi']] Output: flist = [['Canada', 'France', 'London', 'Machine'], ['Munich', 'Spain'], ['Australia', 'Mandi']]

There are multiple ways to sort each list in alphabetical order.

Method #1 : Using map




# Python code to sort all sublists
# in given list of strings
# List initialization
Input = [['Machine', 'London', 'Canada', 'France', 'Lanka'],
['Spain', 'Munich'],
['Australia', 'Mandi']]
# Using map for sorting
Output = list(map(sorted, Input))
# Printing output
print(Output)
Output:

[[‘Canada’, ‘France’, ‘Lanka’, ‘London’, ‘Machine’], [‘Munich’, ‘Spain’], [‘Australia’, ‘Mandi’]]


Method #2 : Using lambda and sorted




# Python code to sort all sublists
# in given list of strings
# List initialization
Input = [['Machine', 'London', 'Canada', 'France', 'Lanka'],
['Spain', 'Munich'],
['Australia', 'Mandi']]
# using lambda and sorted
Output = [sorted(x, key = lambda x:x[0]) for x in Input]
# Printing output
print(Output)
Output:

[[‘Canada’, ‘France’, ‘London’, ‘Lanka’, ‘Machine’], [‘Munich’, ‘Spain’], [‘Australia’, ‘Mandi’]]


Method #3 : Using iteration and sort




# Python code to sort all sublists
# in given list of strings
# List initialization
Input = [['Machine', 'London', 'Canada', 'France', 'Lanka'],
['Spain', 'Munich'],
['Australia', 'Mandi']]
# sorting sublist
for sublist in Input:
sublist.sort()
# Printing output
print(Input)
Output:

[[‘Canada’, ‘France’, ‘Lanka’, ‘London’, ‘Machine’], [‘Munich’, ‘Spain’], [‘Australia’, ‘Mandi’]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Python | Sort list of lists by the size of sublists

Given a list of lists, the task is to sort a list on the basis of size of sublists. Let’s discuss a few methods to do the same.

Method #1: Using sort




# Python code to demonstrate
# sort list of list
# on the basis of size of sublist
ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4],
[1, 2, 3, 4, 5], [2, 4, 6]]
# printing initial ini_list
print ("initial list", str(ini_list))
# sorting on bais of size of list
ini_list.sort(key = len)
# printing final result
print("final list", str(ini_list))
Output:

initial list [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]]
final list [[1, 2], [1, 2, 3], [2, 4, 6], [1, 2, 3, 4], [1, 2, 3, 4, 5]]


Method #2: Using lambda






# Python code to demonstrate
# sort list of list
# on the basis of size of sublist
ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4],
[1, 2, 3, 4, 5], [2, 4, 6]]
# printing initial ini_list
print ("initial list", str(ini_list))
# sorting on bais of size of list
ini_list.sort(key = lambda x:len(x))
# printing final result
print("final list", str(ini_list))
Output:

initial list [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]]
final list [[1, 2], [1, 2, 3], [2, 4, 6], [1, 2, 3, 4], [1, 2, 3, 4, 5]]


Method #3: Using sorted




# Python code to demonstrate
# sort list of list
# on the basis of size of sublist
ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4],
[1, 2, 3, 4, 5], [2, 4, 6]]
# printing initial ini_list
print ("initial list", str(ini_list))
# sorting on bais of size of list
result = sorted(ini_list, key = len)
# printing final result
print("final list", str(result))
Output:

initial list [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]]
final list [[1, 2], [1, 2, 3], [2, 4, 6], [1, 2, 3, 4], [1, 2, 3, 4, 5]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
python-list
Python-list-of-lists
Python-sort
Practice Tags :
python-list
Read Full Article

Sort a List of Lists in Python

Python Python List

Created: June-02, 2021

A list is one of the most powerful data structures used in Python. We can sort a list in Python by arranging all its elements in ascending or descending order based on the requirement.

We can also have nested lists in Python. These can be thought of as a list of lists. Sorting a list of lists arranges all the inner lists according to the specified index as the key.

In this tutorial, we will sort a list of lists in Python based on some indexes.

Different ways of Sorting the list of lists in python

  1. Sorting the data by 1st column
  2. Sorting the data using any other column
  3. Sorting the list of lists by length
  4. How to sort the list of lists by the sum of elements
  5. Sorting the list of lists in descending order
  6. Creating our own Program to sort the list of lists in Python

1. Sorting the data by 1st column

To sort the data according to the 1st columns in ascending order.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] # Using the key parameter of sort method. # if we use the sort method, the sorting take place in the list itself list1.sort() print(list1)

Output-

[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'rtr 160', 50], ['Yamaha', 'YZF R15 Ver 3.0', 45]]

When we want to sort the list according to the first columns, we do not need to do anything special. We just need to do everything as usual. Let us see another way of achieving the same.

The above method sorts the data in the original list itself. If we do not wish to make the changes in the original list, we can use the below method.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] print(sorted(list1)) # lets see if our original list has changed or not print(list1)
[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'rtr 160', 50], ['Yamaha', 'YZF R15 Ver 3.0', 45]]

Now let us see the naive method using to achieve our goal.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] # Iterating through one the list. #There is no element with which we will compare the last element, #so we are not including the last element for i in range(len(list1)-1): # if the next element is greater then the next element, swap it. if list1[i][0]>list1[i+1][0]: list1[i][0],list1[i+1][0]=list1[i+1][0],list1[i][0] print(list1)
[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'YZF R15 Ver 3.0', 45], ['Yamaha', 'rtr 160', 50]]

Python List sort()

In this tutorial, we will learn about the Python sort() method with the help of examples.

The sort() method sorts the elements of a given list in a specific ascending or descending order.

Example

prime_numbers = [11, 3, 7, 5, 2]
# sort the list prime_numbers.sort()
print(prime_numbers) # Output: [2, 3, 5, 7, 11]

Video liên quan

Postingan terbaru

LIHAT SEMUA