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
# 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") |
chevron_right
filter_none
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
# 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") |
chevron_right
filter_none
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
Recommended Posts:
- Python | Unpacking tuple of lists
- Python | Convert a list into tuple of lists
- Python | Ways to iterate tuple list of lists
- Python | Find whether all tuple have same length
- Python | Program to count number of lists in a list of lists
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Find common elements in list of lists
- Python | Find missing and additional values in two lists
- Python | Find minimum of each index in list of lists
- Python program to find common elements in three lists using sets
- Python | Find frequency of given character at every position in list of lists
- Python | tuple() Function
- Python | Reversing a Tuple
- Unpacking a Tuple in Python
- Python | Removing strings from tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



