Python – Value list lengths
Last Updated :
10 Mar, 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 length 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 length is : " + str (res))
|
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list
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 length is : " + str (res))
|
Time complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Here is an approach to solve this problem using reduce():
Python3
from functools import reduce
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 2 ]), ( 'key3' , [ 9 , 3 ])]
print ( "The original list : " + str (test_list))
result = reduce ( lambda acc, x: acc + [(x[ 0 ], len (x[ 1 ]))], test_list, [])
print ( "The list tuple attribute length is : " + str (result))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
The list tuple attribute length is : [('key1', 3), ('key2', 3), ('key3', 2)]
The reduce() function applies the lambda function to the elements of the test_list, and accumulates the result in a new list. The time complexity of this approach is O(n) and space complexity is O(n)
This approach first pass the test_list to the reduce function and initial value of empty list and the lambda function will take 2 arguments acc, x where acc is the accumulation of the result and x is the current element of the list, lambda function will append the tuple of first element and the length of the second element of the tuple to the acc and returns acc. This process will be repeated for all elements of the list and final result will be the list of tuple of first element and the length of the second element of the tuple.
Method #3: Using min()and for loop:
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 3 ]), ( 'key3' , [ 9 , 3 , 2 ])]
print ( "The original list : " + str (test_list))
res = []
for tup in test_list:
key = tup[ 0 ]
sublist = tup[ 1 ]
min_val = min (sublist)
res.append((key, min_val))
print ( "The list tuple attribute minimum is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 3]), ('key3', [9, 3, 2])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 2)]
time complexity: O(n)
space complexity :O(n)
Method #4:Using enumeration
Algorithm:
- Create an empty list to store results.
- Iterate over the test_list using enumerate() to get index and tuple of each element.
- Extract the key and sublist from the tuple.
- Find the minimum value of sublist using the min() function.
- Append the tuple with key and minimum value as a tuple to the result list.
- Print the result list.
Python3
test_list = [( 'key1' , [ 3 , 4 , 5 ]), ( 'key2' , [ 1 , 4 , 3 ]), ( 'key3' , [ 9 , 3 , 2 ])]
print ( "The original list : " + str (test_list))
res = [(key, min (sublist)) for idx, (key, sublist) in enumerate (test_list)]
print ( "The list tuple attribute minimum is : " + str (res))
|
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 3]), ('key3', [9, 3, 2])]
The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 2)]
Time complexity: O(n), where n is the length of the input list. We are iterating over the list once using a loop.
Auxiliary Space: O(n), where n is the length of the input list. We are creating a list to store the results.
Share your thoughts in the comments
Please Login to comment...