{"id":10024,"date":"2021-09-30T17:30:47","date_gmt":"2021-09-30T12:00:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=10024"},"modified":"2021-11-22T18:33:32","modified_gmt":"2021-11-22T13:03:32","slug":"python-program-to-find-the-length-of-a-list-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-length-of-a-list-using-recursion\/","title":{"rendered":"Python Program to Find the Length of a List Using Recursion"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Recursion:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Lists in Python:<\/strong><\/p>\n A list is a built-in Python data structure that stores a collection of things. Lists have a number of key features:<\/p>\n Given a list, the task is to calculate the length of the given list recursively in Python<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the length of the given list in python using the recursive approach:<\/p>\n When it is necessary to find the length of a list using recursion, a user-defined algorithm and a simple indexing method are utilized. Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n i)Integer List<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)String List<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\n\n
given list = [929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]<\/pre>\n
The given list is :\r\n[929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]\r\nThe length of the given list = 20<\/pre>\n
given list = ['hello', 'this', 'btechgeeks', 'python']<\/pre>\n
The given list is :\r\n['hello', 'this', 'btechgeeks', 'python']\r\nThe length of the given list = 4<\/pre>\n
Program to Find the Length of a List Using Recursion in Python<\/h2>\n
\n
\nThe recursion computes the output of little pieces of the larger problem and combines them to produce the answer to the larger problem.<\/p>\n1)Using Recursion(Static Input)<\/h3>\n
\n
# function which accepts the list as argument and\r\n# calculates the length of the given list and return it\r\n\r\n\r\ndef listlengthRecursion(given_list):\r\n # Put a condition in the method that says if it\r\n # isn't the original list, it should return 0.\r\n if not given_list:\r\n return 0\r\n # Otherwise, execute the function recursively to determine the length of the list.\r\n return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])\r\n\r\n\r\n# Give the list input as static input and store it in a variable.\r\ngiven_list = [929, 138, 3, 193, 11, 1, 2, 3, 41,\r\n 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]\r\n# printing the given list\r\nprint('The given list is :')\r\nprint(given_list)\r\n# Pass the given list as an argument to the recursive function which\r\n# calculates the length of the given list and returns it.\r\nprint(\"The length of the given list = \", listlengthRecursion(given_list))\r\n<\/pre>\n
The given list is :\r\n[929, 138, 3, 193, 11, 1, 2, 3, 41, 132, 56, 11, 917, 212, 455, 2235, 1, 1, 2, 3]\r\nThe length of the given list = 20<\/pre>\n
\n
2)Using Recursion(User Input)<\/h3>\n
\n
# function which accepts the list as argument and\r\n# calculates the length of the given list and return it\r\n\r\n\r\ndef listlengthRecursion(given_list):\r\n # Put a condition in the method that says if it\r\n # isn't the original list, it should return 0.\r\n if not given_list:\r\n return 0\r\n # Otherwise, execute the function recursively to determine the length of the list.\r\n return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])\r\n\r\n\r\n# Give the list as user input (integer list) using the map(),int(),split() and list()\r\n# functions and store in a variable.\r\ngiven_list = list(map(int, input('Enter some random elements of the list separated by spaces ').split()))\r\n# printing the given list\r\nprint('The given list is :')\r\nprint(given_list)\r\n# Pass the given list as an argument to the recursive function which\r\n# calculates the length of the given list and returns it.\r\nprint(\"The length of the given list = \", listlengthRecursion(given_list))\r\n<\/pre>\n
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\r\nThe given list is :\r\n[7, 372, 1, 93, 2, 491, 22, 434, 11, 45, 9, 8, 2, 3, 4]\r\nThe length of the given list = 15<\/pre>\n
\n
# function which accepts the list as argument and\r\n# calculates the length of the given list and return it\r\n\r\n\r\ndef listlengthRecursion(given_list):\r\n # Put a condition in the method that says if it\r\n # isn't the original list, it should return 0.\r\n if not given_list:\r\n return 0\r\n # Otherwise, execute the function recursively to determine the length of the list.\r\n return 1 + listlengthRecursion(given_list[1::2]) + listlengthRecursion(given_list[2::2])\r\n\r\n\r\n# Give the list as user input (String list) using the split() and list() functions and store in a variable.\r\ngiven_list = list( input('Enter some random elements of the list separated by spaces =').split())\r\n# printing the given list\r\nprint('The given list is :')\r\nprint(given_list)\r\n# Pass the given list as an argument to the recursive function which\r\n# calculates the length of the given list and returns it.\r\nprint(\"The length of the given list = \", listlengthRecursion(given_list))\r\n<\/pre>\n
Enter some random elements of the list separated by spaces =hello this is bychg btechgeeeks\r\nThe given list is :\r\n['hello', 'this', 'is', 'bychg', 'btechgeeeks']\r\nThe length of the given list = 5<\/pre>\n
\n