Open In App

Python | Row lengths in Matrix

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the lengths of rows of matrix in uneven sized matrix. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using max() + map() + sum() + list comprehension The combination of above functions can help to get the solution to this particular problem in just a one line and hence quite useful. The sum function computes the sum of sublists, max function can be used to order in descending and all this bound together using list comprehension. 

Python3




# Python3 code to demonstrate
# Row lengths in matrix
# using max() + map() + sum() + list comprehension
 
# initializing list
test_list = [[4, 5, 6], [7, 8], [2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using max() + map() + sum() + list comprehension
# Row lengths in matrix
res = [sum(len(row) > idx for row in test_list)
    for idx in range(max(map(len, test_list)))]
 
# print result
print("The row lengths in matrix : " + str(res))


Output : 

The original list : [[4, 5, 6], [7, 8], [2]]
The row lengths in matrix : [3, 2, 1]

Method #2 : Using sum() + filter() + zip_longest() This problem can also be solved using the set of functions above. The filter function can be used to get the separate lists and the task of binding for summation done by sum function is performed by zip_longest function. 

Python3




# Python3 code to demonstrate
# Row lengths in matrix
# using sum() + filter() + zip_longest()
from itertools import zip_longest
 
# initializing list
test_list = [[4, 5, 6], [7, 8], [2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using sum() + filter() + zip_longest()
# Row lengths in matrix
res = [sum(1 for idx in filter(None.__ne__, i))
              for i in zip_longest(*test_list)]
 
# print result
print("The row lengths in matrix : " + str(res))


Output : 

The original list : [[4, 5, 6], [7, 8], [2]]
The row lengths in matrix : [3, 2, 1]

Method #3: Using for loop and append()

We can use a for loop to iterate over the rows of the matrix and append the length of each row to a new list.

Step by step Algorithm:

  • Initialize an empty list “res”.
  • Traverse through each row of the input list using a for loop.
  • Calculate the length of each row using the “len()” function.
  • Append the length of each row to the “res” list using the “append()” function.
  • Print the “res” list as the output.

Python3




# initializing list
test_list = [[4, 5, 6], [7, 8], [2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using for loop and append()
# Row lengths in matrix
res = []
for row in test_list:
    res.append(len(row))
 
# print result
print("The row lengths in matrix : " + str(res))


Output

The original list : [[4, 5, 6], [7, 8], [2]]
The row lengths in matrix : [3, 2, 1]

Time Complexity: O(n), where “n” is the total number of elements in the input list.
Auxiliary Space: O(n), where “n” is the total number of elements in the input list. This is because we are storing the length of each row in the “res” list.

Method #4: Using List Comprehension and len()

  • Initialize the original list
  • Use list comprehension to iterate over each sublist and compute its length using len() function
  • Store the length of each sublist in a new list using the list comprehension
  • Print the row lengths in matrix

Python3




## initializing list
test_list = [[4, 5, 6], [7, 8], [2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using List Comprehension and len()
# Row lengths in matrix
res = [len(row) for row in test_list]
 
# print result
print("The row lengths in matrix : " + str(res))


Output

The original list : [[4, 5, 6], [7, 8], [2]]
The row lengths in matrix : [3, 2, 1]

Time Complexity: O(n), where n is the number of sublists in the original list
Auxiliary Space: O(n), where n is the number of sublists in the original list



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads