Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Recursion in Python:
Python also supports function recursion, which means that a specified function can call itself.
Recursion is a mathematical and programming concept that is widely used. It signifies that a function calls itself. This has the advantage of allowing you to loop through data to obtain a result.
The developer must exercise extreme caution when using recursion since it is quite easy to write a function that never terminates or consumes excessive amounts of memory or computing power. However, when performed correctly, recursion may be a tremendously efficient and mathematically elegant way to programming.
Nested List:
A list can include any form of the object, including another list (sublist), which can contain sublists, and so on. This is referred to as a nested list.
Given a nested list, the task is to write a python program that converts the given list to flatted list.
Examples:
I)Integer Nested List
Example1:
Input:
given Nested List = [ [7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23],[3, 23, 124] ]
Output:
Printing the given nested list : [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]] Printing the flattened list : [7, 8, 2, 11, 19, 29, 63, 91, 12, 52, 98, 11, 19, 18, 11, 28, 34, 39, 35, 56, 87, 46, 1, 2, 5, 9, 23, 3, 23, 124]
ii)String Nested List
Example2:
Input:
given Nested List = [ ['hello', 'this'], ['btechgeeks', 'online', 'python'], [ 'programming', 'and'], ['learning', 'platform'], ['for'], ['coding', 'students']]
Output:
Printing the given nested list : [['hello', 'this'], ['btechgeeks', 'online', 'python'], ['programming', 'and'], ['learning', 'platform'], ['for'], ['coding', 'students']] Printing the flattened list : ['hello', 'this', 'btechgeeks', 'online', 'python', 'programming', 'and', 'learning', 'platform', 'for', 'coding', 'students']
iii)Random Nested List(Elements can be of any data type)
Example3:
Input:
given nested list = [[928.4, 1], ['btechgeeks', 'online', True], ['programming', 'and'], ['learning', 'platform'], ['for'], [False, 'students'], [4, 18]]
Output:
Printing the given nested list : [[928.4, 1], ['btechgeeks', 'online', True], ['programming', 'and'], ['learning', 'platform'], ['for'], [False, 'students'], [4, 18]] Printing the flattened list : [928.4, 1, 'btechgeeks', 'online', True, 'programming', 'and', 'learning', 'platform', 'for', False, 'students', 4, 18]
Program to Flatten a Nested List using Recursion in Python
Below are the ways to Flatten a Nested List using recursion in Python.
Approach:
- Give the nested list as static input and store it in a variable.
- To flatten the list, pass it as an argument to a recursive function.
- If the list is empty, the function returns the list.
- Otherwise, use the function with the sublists as parameters recursively until the entire list is flattened.
- Print the list that has been flattened.
- The exit of the program.
1)Integer Nested List
Below is the implementation:
# function which takes the nested list as argument and # returns the flattened list using recursion def flattenListRecursion(given_NestedList): # If the list is empty, the function returns the list. if given_NestedList == []: return given_NestedList # Otherwise, use the function with the sublists as parameters # recursively until the entire list is flattened. if isinstance(given_NestedList[0], list): return flattenListRecursion(given_NestedList[0]) + flattenListRecursion(given_NestedList[1:]) return given_NestedList[:1] + flattenListRecursion(given_NestedList[1:]) # Give the nested list as static input and store it in a variable. givenNestedList = [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]] # printing the given nested list print('Printing the given nested list : \n', givenNestedList) # passing the given nested list as an argument to the recursive function flattenListRecursion # which returns the flattened lsit print("Printing the flattened list : \n", flattenListRecursion(givenNestedList))
Output:
Printing the given nested list : [[7, 8, 2], [11, 19, 29, 63, 91], [12, 52, 98], [11, 19, 18], [11, 28, 34, 39, 35, 56, 87, 46, 1, 2], [5], [9, 23], [3, 23, 124]] Printing the flattened list : [7, 8, 2, 11, 19, 29, 63, 91, 12, 52, 98, 11, 19, 18, 11, 28, 34, 39, 35, 56, 87, 46, 1, 2, 5, 9, 23, 3, 23, 124]
Explanation:
- A nested list is initialized in a variable.
- To flatten the list, the list is given as an argument to a recursive function.
- If the list is empty, the function returns the list.
- Otherwise, the function is repeated recursively with the sublists as arguments until the entire list is flattened.
- The list has been flattened and printed.
2)String Nested List
Below is the implementation:
# function which takes the nested list as argument and # returns the flattened list using recursion def flattenListRecursion(given_NestedList): # If the list is empty, the function returns the list. if given_NestedList == []: return given_NestedList # Otherwise, use the function with the sublists as parameters # recursively until the entire list is flattened. if isinstance(given_NestedList[0], list): return flattenListRecursion(given_NestedList[0]) + flattenListRecursion(given_NestedList[1:]) return given_NestedList[:1] + flattenListRecursion(given_NestedList[1:]) # Give the nested list as static input and store it in a variable. givenNestedList = [['hello', 'this'], ['btechgeeks', 'online', 'python'], [ 'programming', 'and'], ['learning', 'platform'], ['for'], ['coding', 'students']] # printing the given nested list print('Printing the given nested list : \n', givenNestedList) # passing the given nested list as an argument to the recursive function flattenListRecursion # which returns the flattened lsit print("Printing the flattened list : \n", flattenListRecursion(givenNestedList))
Output:
Printing the given nested list : [['hello', 'this'], ['btechgeeks', 'online', 'python'], ['programming', 'and'], ['learning', 'platform'], ['for'], ['coding', 'students']] Printing the flattened list : ['hello', 'this', 'btechgeeks', 'online', 'python', 'programming', 'and', 'learning', 'platform', 'for', 'coding', 'students']
3)Random Nested List(Elements can be of any data type)
Below is the implementation:
# function which takes the nested list as argument and # returns the flattened list using recursion def flattenListRecursion(given_NestedList): # If the list is empty, the function returns the list. if given_NestedList == []: return given_NestedList # Otherwise, use the function with the sublists as parameters # recursively until the entire list is flattened. if isinstance(given_NestedList[0], list): return flattenListRecursion(given_NestedList[0]) + flattenListRecursion(given_NestedList[1:]) return given_NestedList[:1] + flattenListRecursion(given_NestedList[1:]) # Give the nested list as static input and store it in a variable. givenNestedList = [[928.4, 1], ['btechgeeks', 'online', True], [ 'programming', 'and'], ['learning', 'platform'], ['for'], [False, 'students'], [4, 18]] # printing the given nested list print('Printing the given nested list : \n', givenNestedList) # passing the given nested list as an argument to the recursive function flattenListRecursion # which returns the flattened lsit print("Printing the flattened list : \n", flattenListRecursion(givenNestedList))
Output:
Printing the given nested list : [[928.4, 1], ['btechgeeks', 'online', True], ['programming', 'and'], ['learning', 'platform'], ['for'], [False, 'students'], [4, 18]] Printing the flattened list : [928.4, 1, 'btechgeeks', 'online', True, 'programming', 'and', 'learning', 'platform', 'for', False, 'students', 4, 18]
Related Programs:
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Find the Power of a Number Using Recursion
- Python Program to Find the LCM of Two Numbers Using Recursion
- Python Program to Find the Length of a List Using Recursion
- Python Program to Reverse a String Using Recursion