Python – Elements Lengths in List
Last Updated :
01 May, 2023
Sometimes, while working with Python lists, can have problem in which we need to count the sizes of elements that are part of lists. This is because list can allow different element types to be its members. This kind of problem can have application in many domains such has day-day programming and web development. Lets discuss certain ways in which we can perform this task.
Method #1 : Using loop This is brute way in which this task can be performed. In this, we iterate for each loop element and find its size using elements counter.
Python3
test_list = [ 'GFG' , ( 4 , 5 , 6 ), 17 , [ 5 , 6 , 7 , 8 ], 'Best' ]
print ("The original list is : " + str (test_list))
res = []
for ele in test_list:
count = 0
if type (ele) = = int :
res.append( 1 )
else :
for sub in ele:
count = count + 1
res.append(count)
print ("The element sizes in order are : " + str (res))
|
Output :
The original list is : ['GFG', (4, 5, 6), 17, [5, 6, 7, 8], 'Best']
The element sizes in order are : [3, 3, 1, 4, 4]
Time Complexity: O(n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2 : Using list comprehension + len() This task can also be performed using list comprehension. In this, we compute length using len() and iteration is performed using list comprehension.
Python3
test_list = [ 'GFG' , ( 4 , 5 , 6 ), 17 , [ 5 , 6 , 7 , 8 ], 'Best' ]
print ("The original list is : " + str (test_list))
res = [ len (sub) if type (sub) ! = int else 1 for sub in test_list]
print ("The element sizes in order are : " + str (res))
|
Output :
The original list is : ['GFG', (4, 5, 6), 17, [5, 6, 7, 8], 'Best']
The element sizes in order are : [3, 3, 1, 4, 4]
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #3 : Using len()+ append()
This method defines a function element_lengths that takes a list as input and returns a list of the lengths of the elements in the input list. The function loops through each element in the input list and checks its type using the isinstance function. If the element is a string, tuple, or list, the length of the element is appended to the result list. If the element is not a string, tuple, or list, 1 is appended to the result list. Finally, the result list is returned. The code also includes a couple of comments to explain what each part of the code is doing.
Python3
def element_lengths(test_list):
result = []
for elem in test_list:
if isinstance (elem, str ):
result.append( len (elem))
elif isinstance (elem, tuple ):
result.append( len (elem))
elif isinstance (elem, list ):
result.append( len (elem))
else :
result.append( 1 )
return result
test_list = [ 'GFG' , ( 4 , 5 , 6 ), 17 , [ 5 , 6 , 7 , 8 ], 'Best' ]
print (element_lengths(test_list))
|
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using numpy:
Algorithm:
1.Import the NumPy library.
2.Define the original list.
3.Print the original list.
4.Use a list comprehension to create a list of the lengths of each element in the original list, except for integers 5.which are represented as having a length of 1.
6.Convert the resulting list to a NumPy array.
7.Print the resulting array as a list using the tolist() method.
Python3
import numpy as np
test_list = [ 'GFG' , ( 4 , 5 , 6 ), 17 , [ 5 , 6 , 7 , 8 ], 'Best' ]
print ( "The original list is : " + str (test_list))
res = np.array([ len (elem) if type (elem) ! = int else 1 for elem in test_list])
print ( "The element sizes in order are : " + str (res.tolist()))
|
Output:
The original list is : ['GFG', (4, 5, 6), 17, [5, 6, 7, 8], 'Best']
The element sizes in order are : [3, 3, 1, 4, 4]
Time complexity: The algorithm loops through each element of the input list once, so the time complexity is O(n), where n is the length of the input list.
Auxiliary Space: The algorithm creates a NumPy array to store the result, so the space complexity is O(n), where n is the length of the input list. However, since the size of the array is directly proportional to the size of the input list, we can consider the space complexity to be O(1) in terms of the size of the input list.
Method #5: Using map() function
Python3
test_list = [ 'GFG' , ( 4 , 5 , 6 ), 17 , [ 5 , 6 , 7 , 8 ], 'Best' ]
print ( "The original list is : " + str (test_list))
res = list ( map ( lambda sub: len (sub) if type (sub) ! = int else 1 , test_list))
print ( "The element sizes in order are : " + str (res))
|
Output
The original list is : ['GFG', (4, 5, 6), 17, [5, 6, 7, 8], 'Best']
The element sizes in order are : [3, 3, 1, 4, 4]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we are creating a new list of the same length as the input list.
Please Login to comment...