GeeksforGeeks App
Open App
Browser
Continue

# Python | Find minimum of each index in list of lists

Sometimes, we have encountered such problems in which we need to find the minimum of each column in a matrix i.e minimum of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Let’s discuss certain ways in which this problem can be solved.

Examples:

```Input : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
Output : [1, 3, 2]

Input : [[3, 2, 8], [5, 3, 5], [9, 3, 1]]
Output : [3, 2, 1]```

Method #1: Using min() + list comprehension + zip() The combination of above methods are required to solve this particular problem. The min function is used to get the required minimum value and zip function provides the combination of like indices and then list is created using list comprehension.

## Python3

 `# Python3 code to demonstrate``# Minimum index value``# using min() + list comprehension + zip()` `# initializing list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]` `# printing original list``print``("The original ``list` `: " ``+` `str``(test_list))` `# using min() + list comprehension + zip()``# Minimum index value``res ``=` `[``min``(idx) ``for` `idx ``in` `zip``(``*``test_list)]` `# print result``print``("The Minimum of each index ``list` `is` `: " ``+` `str``(res))`

Output :

```The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]```

Time Complexity: O(n*m) where n is the number of sublists in the test_list and m is the length of the sublists.
Auxiliary Space: O(n) where n is the number of sublists in the test_list.

Method #2 : Using map() + min() + zip() This works in almost similar way as the above method, but the difference is just that we use map function to build the min element list rather than using list comprehension.

## Python3

 `# Python3 code to demonstrate``# Minimum index value``# using min() + map() + zip()` `# initializing list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]` `# printing original list``print``("The original ``list` `: " ``+` `str``(test_list))` `# using min() + map() + zip()``# Minimum index value``res ``=` `list``(``map``(``min``, ``zip``(``*``test_list)))` `# print result``print``("The Minimum of each index ``list` `is` `: " ``+` `str``(res))`

Output :

```The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]```

Time complexity: O(mn), where m is the number of sublists in the original list and n is the length of each sublist. This is because the zip function takes O(min(m, n)) time to transpose the sublists, the map function takes O(mn) time to apply the min function to each index in the sublists, and the list function takes O(m*n) time to convert the result to a list.
Auxiliary space: O(mn), as it requires a list of size mn to store the result.

Method #3 : Using enumerate()

The function get_min_at_each_index takes in a list of lists lst. It initializes an empty list min_at_each_index to store the minimum value at each index.

It then iterates through each index i in the range 0 to the length of the first list in lst (since all the lists in lst are assumed to have the same length). For each index i, it appends to min_at_each_index the minimum value of the i-th element from each list in lst. It does this by using a list comprehension to create a list of the i-th elements from each list in lst, and then passing this list to the min function.

## Python3

 `# Python3 code to demonstrate``# Minimum index value``def` `get_min_at_each_index(lst):``    ``min_at_each_index ``=` `[]``    ``for` `i ``in` `range``(``len``(lst[``0``])):``        ``min_at_each_index.append(``min``(row[i] ``for` `row ``in` `lst))``    ``return` `min_at_each_index``# initializing list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# print result``print``(``"The Minimum of each index list is : "` `, get_min_at_each_index(test_list))``#This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is :  [1, 3, 2]```

Time complexity: O(n), where n is number of elements in all lists
Auxiliary Space: O(n), to store result

Method #4 : Using min()+for loops

## Python3

 `# Python3 code to demonstrate``# Minimum index value` `# initializing list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))`  `# Minimum index value``res``=``[]``for` `i ``in` `range``(``0``,``len``(test_list)):``    ``x``=``[]``    ``for` `j ``in` `range``(``0``,``len``(test_list[i])):``        ``x.append(test_list[j][i])``    ``res.append(``min``(x))` `# print result``print``(``"The Minimum of each index list is : "` `+` `str``(res))`

Output

```The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]```

Time complexity: O(n*n), where n is a number of elements in all lists
Auxiliary Space: O(n), to store result

Using NumPy() library

## Python3

 `import` `numpy as np` `# initializing list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using numpy``res ``=` `np.amin(test_list, axis``=``0``)` `# print result``print``(``"The Minimum of each index list is : "` `+` `str``(res))``#This code is contributed Vinay Pinjala.`

Output:

```The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]```

Time complexity: O(n*n), where n is the number of elements in all lists
Auxiliary Space: O(n), to store result

Method  : Using  map() and lambda function:

1.Define a list of lists called “test_list”.
2.Print the original list using the “print()” function.
3.Use the map function with a lambda function to find the minimum value at each index:
a. Define a lambda function that takes in multiple arguments using the “*” operator.
b. Use the built-in “min()” function to find the minimum value of the input arguments.
c. Apply the lambda function to each set of values in the sublists using the map() function.
d. Convert the resulting map object to a list using the “list()” function.
4.Store the list of minimum values in a new variable called “min_index_values”.
5.Print the minimum of each index list using the “print()” function.

## Python3

 `# Define the test list``test_list ``=` `[[``3``, ``7``, ``6``], [``1``, ``3``, ``5``], [``9``, ``3``, ``2``]]``# Print the original list``print``(``"The original list:"``, test_list)``# Use the map function with a lambda function to find the minimum value at each index``min_index_values ``=` `list``(``map``(``lambda` `*``args: ``min``(args), ``*``test_list))``# Print the minimum of each index list``print``(``"The minimum of each index list is:"``, min_index_values)``#This code is contributed by Jyothi pinjala.`

Output

```The original list: [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The minimum of each index list is: [1, 3, 2]
```

Time Complexity:
The map() function with a lambda function is used to find the minimum value at each index in the list of lists. Since the map() function has to iterate over all the elements of the input list, its time complexity is O(N), where N is the total number of elements in the list of lists. The use of the built-in “min()” function has a time complexity of O(k), where k is the number of arguments passed to the function. In this case, k is the number of sublists in the input list. Therefore, the time complexity of the code is O(N*k).

Space Complexity:
The space complexity of the code is O(k), where k is the number of sublists in the input list. This is because the map() function returns a map object, which is converted to a list using the “list()” function. The resulting list contains the minimum values at each index of the sublists. Since the size of this list is proportional to the number of sublists in the input list, the space complexity of the code is O(k).

My Personal Notes arrow_drop_up