Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Recursion Functions

Recursive function to print elements of a list - Python Program

Example 1 :

def print_list(lst, index=0): """Print elements of a list recursively.""" if index == len(lst): return print(lst[index]) print_list(lst, index+1) print_list([1, 2, 3, 4])

Output

 
OUTPUT  :
1
2
3
4

Example 2 : Advanced Program

def recursive_print_list(data_list): """ Recursively prints each element of a list. """ # Base Case: If the list is empty, stop the recursion. if not data_list: return # Recursive Step: Print the first element and then # call the function recursively with the rest of the list. print(data_list[0]) recursive_print_list(data_list[1:]) # Example Usage my_list = [10, 20, 30, 40, 50] print("Printing list elements recursively:") recursive_print_list(my_list)

Output

 
OUTPUT  :
Printing list elements recursively:
10
20
30
40
50