Python | Find number of lists in a tuple
Last Updated :
02 Jan, 2023
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
# Python code to find number of list in a tuple
# Initial list
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])
# Using len to find no of list in tuple
Output1 = len(Input1)
Output2 = len(Input2)
Output3 = len(Input3)
# Printing
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
# Python code to find number of list in a tuple
# Using find function
def find(Input):
if isinstance(Input, list):
return 1
else:
return len(Input)
# List initialization
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])
# using find
Output1 = find(Input1)
Output2 = find(Input2)
Output3 = find(Input3)
# printing
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
# Python code to find number of list in a tuple
# Initial list
Input1 = ([1, 2, 3, 4], [5, 6, 7, 8],5)
# Using type() to find no of list in tuple
Output1=0
for i in Input1:
if(type(i) is list):
Output1+=1
# Printing
print("Initial list :")
print(Input1)
print("No of list in tuples are :")
print(Output1)
OutputInitial 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 the number of elements that are lists using the type() and issubclass() functions
return sum([1 for element in t if element.__class__ == list])
# List initialization
Input1 = ([1, 2, 3, 4], [5, 6, 7, 8])
Input2 = ([1, 2], [3, 4], [5, 6])
# Find the number of lists in each tuple
print("Initial list :")
print(Input1)
print("No of list in tuples are :")
print(find_num_lists(Input1)) # Output: 2
print("Initial list :")
print(Input2)
print("No of list in tuples are :")
print(find_num_lists(Input2)) # Output: 3
OutputInitial 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)
Similar Reads
Counting number of unique values in a Python list
Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates.Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once.Pythonli = [1,
2 min read
Merge Two Lists into List of Tuples - Python
The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
3 min read
Sort Tuple of Lists in Python
The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
3 min read
Python - Convert a list into tuple of lists
When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ].Using print()print() function is the
2 min read
Find the size of a list - Python
In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get
2 min read
How to Get the Number of Elements in a Python List
In Python, lists are one of the most commonly used data structures to store an ordered collection of items.In this article, we'll learn how to find the number of elements in a given list with different methods.ExampleInput: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7Let's explore various ways to do
3 min read
Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Memory Management in Lists and Tuples using Python
In Python, lists and tuples are common data structures used to store sequences of elements. However, they differ significantly in terms of memory management, mutability, and performance characteristics. Table of ContentMemory Allocation in ListsMemory Allocation in TuplesComparison of Lists and Tupl
3 min read
Find the Size of a Tuple in Python
There are several ways to find the "size" of a tuple, depending on whether we are interested in the number of elements or the memory size it occupies. For Example: if we have a tuple like tup = (10, 20, 30, 40, 50), calling len(tup) will return 5, since there are five elements in the tuple.Using len
3 min read