Monday, November 25, 2024
Google search engine
HomeLanguagesPython – Value list lengths

Python – Value list lengths

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




# Python3 code to demonstrate
# Value list lengths
# using list comprehension + len()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + len()
# Value list lengths
res = [(key, len(lst)) for key, lst in test_list]
 
# print result
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




# Python3 code to demonstrate
# Value list lengths
# using map() + lambda + len()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + lambda + len()
# Value list lengths
res = list(map(lambda x: (x[0], len(x[1])), test_list))
 
# print result
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




# Python3 code to demonstrate
# Value list lengths
# using reduce()
from functools import reduce
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using reduce()
result = reduce(lambda acc, x: acc + [(x[0], len(x[1]))], test_list, [])
 
# print result
print("The list tuple attribute length is : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy


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




#initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 3]), ('key3', [9, 3,2])]
#printing original list
print("The original list : " + str(test_list))
#using for loop to find minimum value
res = []
for tup in test_list:
    key = tup[0]
    sublist = tup[1]
    min_val = min(sublist)
    res.append((key, min_val))
#print result
print("The list tuple attribute minimum is : " + str(res))
 
#This code contributed by pinjala Jyothi


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:

  1. Create an empty list to store results.
  2. Iterate over the test_list using enumerate() to get index and tuple of each element.
  3. Extract the key and sublist from the tuple.
  4. Find the minimum value of sublist using the min() function.
  5. Append the tuple with key and minimum value as a tuple to the result list.
  6. Print the result list.

Python3




test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 3]), ('key3', [9, 3,2])]
 
#printing original list
print("The original list : " + str(test_list))
 
#using enumeration to find minimum value
res = [(key, min(sublist)) for idx, (key, sublist) in enumerate(test_list)]
 
#print result
print("The list tuple attribute minimum is : " + str(res))
#This code contributed By Vinay Pinjala.


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments