Python | Find number of lists in a tuple
Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application.
Method #1: Using len
Python3
Input1 = ([ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ])
Input2 = ([ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ])
Input3 = ([ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ], [ 1 , 2 , 3 ])
Output1 = len (Input1)
Output2 = len (Input2)
Output3 = len (Input3)
print ("Initial list :")
print (Input1)
print ("No of list in tuples are :")
print (Output1)
print ("\n")
print ("Initial list :")
print (Input2)
print ("No of list in tuples are :")
print (Output2)
print ("\n")
print ("Initial list :")
print (Input3)
print ("No of list in tuples are :")
print (Output3)
print ("\n")
|
Output:
Initial list :
([1, 2, 3, 4], [5, 6, 7, 8])
No of list in tuples are :
2
Initial list :
([1, 2], [3, 4], [5, 6])
No of list in tuples are :
3
Initial list :
([9, 8, 7, 6, 5, 4, 3, 2, 1], [1, 2, 3])
No of list in tuples are :
2
Method #2: Using function and isinstance
Python3
def find( Input ):
if isinstance ( Input , list ):
return 1
else :
return len ( Input )
Input1 = ([ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ])
Input2 = ([ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ])
Input3 = ([ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ])
Output1 = find(Input1)
Output2 = find(Input2)
Output3 = find(Input3)
print ("Initial list :")
print (Input1)
print ("No of list in tuples are :")
print (Output1)
print ("\n")
print ("Initial list :")
print (Input2)
print ("No of list in tuples are :")
print (Output2)
print ("\n")
print ("Initial list :")
print (Input3)
print ("No of list in tuples are :")
print (Output3)
print ("\n")
|
Output:
Initial list :
([1, 2, 3, 4], [5, 6, 7, 8])
No of list in tuples are :
2
Initial list :
([1, 2], [3, 4], [5, 6])
No of list in tuples are :
3
Initial list :
[9, 8, 7, 6, 5, 4, 3, 2, 1]
No of list in tuples are :
1
Method #3: Using type().type() returns the data type of variable.If in case the tuple has different data types other than list, then len() fails.Iterate over tuple and check datatype of each element
Python3
Input1 = ([ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], 5 )
Output1 = 0
for i in Input1:
if ( type (i) is list ):
Output1 + = 1
print ( "Initial list :" )
print (Input1)
print ( "No of list in tuples are :" )
print (Output1)
|
Output
Initial list :
([1, 2, 3, 4], [5, 6, 7, 8], 5)
No of list in tuples are :
2
Method #4: Using __class__
The function find_num_lists() takes in a tuple as an argument and returns the number of elements in the tuple that are lists.
The function uses a list comprehension to iterate over the elements in the tuple and check if the class of the element is list. If it is, it adds 1 to the list. The sum() function is then used to add up all the 1s in the list, which gives the total number of lists in the tuple.
The input tuples Input1 and Input2 are then passed to the find_num_lists() function and the number of lists in each tuple is printed to the console.
Python3
def find_num_lists(t):
return sum ([ 1 for element in t if element.__class__ = = list ])
Input1 = ([ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ])
Input2 = ([ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ])
print ( "Initial list :" )
print (Input1)
print ( "No of list in tuples are :" )
print (find_num_lists(Input1))
print ( "Initial list :" )
print (Input2)
print ( "No of list in tuples are :" )
print (find_num_lists(Input2))
|
Output
Initial list :
([1, 2, 3, 4], [5, 6, 7, 8])
No of list in tuples are :
2
Initial list :
([1, 2], [3, 4], [5, 6])
No of list in tuples are :
3
Time complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
02 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...