- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

When it is required to find the length of a list with the help of recursion technique, a user defined method is used, and simple indexing technique is used.

A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.

Below is a demonstration for the same −

def list_length(my_list): if not my_list: return 0 return 1 + list_length(my_list[1::2]) + list_length(my_list[2::2]) my_list = [1, 2, 3, 11, 34, 52, 78] print("The list is :") print(my_list) print("The length of the string is : ") print(list_length(my_list))

The list is : [1, 2, 3, 11, 34, 52, 78] The length of the string is : 7

- A method named ‘list_length’ is defined, that takes a list as a parameter.
- If the list is not present, the method returns 0.
- Otherwise, it is indexed, and incremented by 1 and returned as output.
- Outside the function, a list is defined, and is displayed on the console.
- The method is called by passing this list as a parameter.
- The output is then displayed on the console.

- Related Questions & Answers
- Python Program to Find the Length of the Linked List using Recursion
- Python Program to Find the Length of the Linked List without using Recursion
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Program to find length of a list without using built-in length() function in Python
- Python Program to Flatten a Nested List using Recursion
- Python Program to Flatten a List without using Recursion
- C++ program for length of a string using recursion
- Python Program to Find the Fibonacci Series Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- C program to find the length of linked list
- Python Program to Find the Fibonacci Series without Using Recursion
- Python Program to find the factorial of a number without recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Display all the Nodes in a Linked List using Recursion

Advertisements