Python – List lengths as record attribute
Last Updated :
18 Apr, 2023
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the lengths of list as tuple attribute. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + len() This particular problem can be solved using list comprehension combined with the len function in which we use len function to find the len of list as a tuple attribute and list comprehension to iterate through the list.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ("The original list : " + str (test_list))
res = [(key, len (lst)) for key, lst in test_list]
print ("The list tuple attribute lengths is : " + str (res))
|
Output :
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute lengths is : [('key1', 3), ('key2', 3), ('key3', 2)]
Time Complexity: O(n), where n is length of test_list.
Auxiliary Space: O(1)
Method #2 : Using map + lambda + len() The above problem can also be solved using the map function to extend the logic to the whole list and len function can perform the similar task as the above method.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ("The original list : " + str (test_list))
res = list ( map ( lambda x: (x[ 0 ], len (x[ 1 ])), test_list))
print ("The list tuple attribute lengths is : " + str (res))
|
Output :
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute lengths is : [('key1', 3), ('key2', 3), ('key3', 2)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the map + lambda + len() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3: Using a for loop
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ( "The original list : " + str (test_list))
res = []
for key, lst in test_list:
res.append((key, len (lst)))
print ( "The list tuple attribute lengths is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute lengths is : [('key1', 3), ('key2', 3), ('key3', 2)]
Time complexity: O(n), where n is the length of the input list of tuples. This is because we are iterating over each tuple in the list once.
Auxiliary space: O(n), where n is the length of the input list of tuples. This is because we are creating a new list to store the results, which has the same length as the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...