• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How do python list items change if they are involved in a recursive loops?

Resolved: How do python list items change if they are involved in a recursive loops?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

This function reverses all the items found in the list. Even the inner lists get reversed. My problem is that I do not understand how the reversed_list keeps 7 and 6 after the recursion. Since once the deep_reverse is called, the reversed_list becomes empty and then it keeps the reverse of the inner list which is [5, 4, 3]. What I do not realize is that once this item is appended the reversed_list becomes [7, 6, [5, 4, 3]]

def deep_reverse(arr):

# Terminaiton / Base condition
if len(arr) < 1: return arr reversed_items = [] # final list to be returned for item in arr[::-1]: # If this item is a list itself, invoke deep_reverse to reverse the items recursively. if type(item) is list: item = deep_reverse(item) # append the item to the final list reversed_items.append(item) print(reversed_items) return reversed_items ``` deep_reverse([1, 2, [3, 4, 5], 6, 7]) #printing the reversed_list [7] [7, 6] [5] [5, 4] [5, 4, 3] [7, 6, [5, 4, 3]] [7, 6, [5, 4, 3], 2] [7, 6, [5, 4, 3], 2, 1] [/code]

Answer:

There’s more than one reversed_list


You say:

once the deep_reverse is called, the reversed_list becomes empty


But there isn’t just one reversed_list. Every call creates a new list (and happens to bind it to a local variable of the same name), but the original call’s reversed_list continues to exist independently of the ones for each recursive call. This is the point of the program’s call stack: Each invocation of a function gets its own space, with its own local variables, separate from that of all other calls (including those to the same function).
Each new call of deep_reverse puts another call frame on top of the existing one, with its own reversed_list. After it finishes populating it, it returns it, and the caller (which may be deep_reverse itself, given the recursion) can do what it likes with it. In the case of your recursive calls, this means rebinding item, which is now bound to the list previously stored in the recursive call’s reversed_list, which can then be appended to the outer call’s own reversed_list.

If you have better answer, please add a comment about this, thank you!

data-structures python recursion recursive-datastructures
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: When converting markdown to latex with pandoc, how can I end a block before the next heading?

26/03/2023

Resolved: TYPO3 SQL error: Field ‘tx_imagezoom_set’ doesn’t have a default value

26/03/2023

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.