Solved: maximum of a list in python recursively

The problem is that the maximum of a list recursively is not always the same as the maximum of the list without recursion.


def maximum(lst): 
  
    if len(lst) == 1: 
        return lst[0] 
    else: 
        return max(lst[0], maximum(lst[1:]))

This is a recursive function to find the maximum value in a list.

If the list has only one element, then that element is the maximum. Otherwise, the maximum is the larger of the first element and the maximum of the rest of the list.

Lists properties

In Python, lists are a data structure that allows you to store a collection of items. Lists can be created using the list() function, and they can be accessed using the index() and len() functions.

Work with lists

In Python, lists are a data structure that allows you to store a collection of items. Lists can be used for a variety of purposes, such as storing data in an organized manner or performing calculations on the items in the list.

To create a list in Python, you use the list() function. To access the first item in a list, you use the index() function. To access the last item in a list, you use the len() function. You can also use the range() function to access specific items in a list.

You can also add items to a list using the append() function. You can remove items from a list using the remove() function. You can also change the order of items in a list using the sort() function.

Related posts:

Leave a Comment