Python Program to Find the Length of a List Using Recursion

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Recursion:

Recursion is a functional technique to problem-solving that involves breaking an issue down into a series of small subproblems with an identical pattern and solving them sequentially by calling one subproblem within another. Recursion is carried out by defining a function capable of solving one subproblem at a time. It calls itself but solves another subproblem somewhere inside that method. As a result, the call to itself continues until some limiting requirements are met.

The main program’s first call to a recursive function will be returned only after all sub calls have completed. As a result, Python keeps the results of all subproblems in temporary memory, does some arithmetic operations (if necessary), and releases the memory at the end of the recursion.

Lists in Python:

A list is a built-in Python data structure that stores a collection of things. Lists have a number of key features:

  • Items in a list are enclosed in square brackets, as in [item1, item2, item3].
  • Lists are ordered, which means that the items in the list appear in a particular order. This allows us to use an index to find any object.
  • Lists are mutable, which means they can be added or removed once they are created.
  • List elements do not have to be distinct. Item duplication is possible since each element has its own individual address and may be accessed independently via the index.
  • Elements can be of many data types: strings, numbers, and objects can all be combined in the same list.

Given a list, the task is to calculate the length of the given list recursively in Python

Examples:

Example1:

Input:

given list = [929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]

Output:

The given list is :
[929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]
The length of the given list =  20

Example2:

Input:

given list = ['hello', 'this', 'btechgeeks', 'python']

Output:

The given list is :
['hello', 'this', 'btechgeeks', 'python']
The length of the given list =  4

Program to Find the Length of a List Using Recursion in Python

Below are the ways to find the length of the given list in python using the recursive approach:

When it is necessary to find the length of a list using recursion, a user-defined algorithm and a simple indexing method are utilized.
The recursion computes the output of little pieces of the larger problem and combines them to produce the answer to the larger problem.

1)Using Recursion(Static Input)

Approach:

  • Give the list input as static input and store it in a variable.
  • Pass the given list as an argument to the recursive function which calculates the length of the given list and returns it.
  • Put a condition in the method that says if it isn’t the original list, it should return 0.
  • Otherwise, execute the function recursively to determine the length of the list.
  • The Recursive function returns the length of the list.
  • Print the length of the given list.
  • The exit of Program.

Below is the implementation:

# function which accepts the list as argument and
# calculates the length of the given list and return it


def listlengthRecursion(given_list):
  # Put a condition in the method that says if it
  # isn't the original list, it should return 0.
    if not given_list:
        return 0
    # Otherwise, execute the function recursively to determine the length of the list.
    return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])


# Give the list input as static input and store it in a variable.
given_list = [929, 138, 3, 193, 11, 1, 2, 3, 41,
              132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]
# printing the given list
print('The given list is :')
print(given_list)
# Pass the given list as an argument to the recursive function which
# calculates the length of the given list and returns it.
print("The length of the given list = ", listlengthRecursion(given_list))

Output:

The given list is :
[929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]
The length of the given list =  20

Explanation:

  • Create a recursive function that accepts a list as an argument.
  • Given the list input as static input and stored it into a variable.
  • Put a condition in the method that says if it isn’t the original list, it should return 0.
  • Otherwise, execute the function recursively to determine the length of the list.
  • Print the result(which is the length of the given list)

2)Using Recursion(User Input)

i)Integer List

Approach:

  • Give the list as user input (integer list) using the map(),int(),split() and list() functions and store in a variable.
  • Pass the given list as an argument to the recursive function which calculates the length of the given list and returns it.
  • Put a condition in the method that says if it isn’t the original list, it should return 0.
  • Otherwise, execute the function recursively to determine the length of the list.
  • The Recursive function returns the length of the list.
  • Print the length of the given list.
  • The exit of Program.

Below is the implementation:

# function which accepts the list as argument and
# calculates the length of the given list and return it


def listlengthRecursion(given_list):
  # Put a condition in the method that says if it
  # isn't the original list, it should return 0.
    if not given_list:
        return 0
    # Otherwise, execute the function recursively to determine the length of the list.
    return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])


# Give the list as user input (integer list) using the map(),int(),split() and list()
# functions and store in a variable.
given_list = list(map(int, input('Enter some random elements of the list separated by spaces ').split()))
# printing the given list
print('The given list is :')
print(given_list)
# Pass the given list as an argument to the recursive function which
# calculates the length of the given list and returns it.
print("The length of the given list = ", listlengthRecursion(given_list))

Output:

Enter some random elements of the list separated by spaces 7 372 1 93 2 491 22 434 11 45 9 8 2 3 4
The given list is :
[7, 372, 1, 93, 2, 491, 22, 434, 11, 45, 9, 8, 2, 3, 4]
The length of the given list = 15

ii)String List

Approach:

  • Give the list as user input (String list) using the split() and list() functions and store in a variable.
  • Pass the given list as an argument to the recursive function which calculates the length of the given list and returns it.
  • Put a condition in the method that says if it isn’t the original list, it should return 0.
  • Otherwise, execute the function recursively to determine the length of the list.
  • The Recursive function returns the length of the list.
  • Print the length of the given list.
  • The exit of Program.

Below is the implementation:

# function which accepts the list as argument and
# calculates the length of the given list and return it


def listlengthRecursion(given_list):
  # Put a condition in the method that says if it
  # isn't the original list, it should return 0.
    if not given_list:
        return 0
    # Otherwise, execute the function recursively to determine the length of the list.
    return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])


# Give the list as user input (String list) using the split() and list() functions and store in a variable.
given_list = list( input('Enter some random elements of the list separated by spaces =').split())
# printing the given list
print('The given list is :')
print(given_list)
# Pass the given list as an argument to the recursive function which
# calculates the length of the given list and returns it.
print("The length of the given list = ", listlengthRecursion(given_list))

Output:

Enter some random elements of the list separated by spaces =hello this is bychg btechgeeeks
The given list is :
['hello', 'this', 'is', 'bychg', 'btechgeeeks']
The length of the given list = 5

Related Programs:

Leave a Comment