Skip to content
Advertisement

Recursive Function to List Comprehension

So I have the following simple function that converts a dictionary to a list of tuples. I was curious if there is a way to do this with list comprehension. I know this would not be good form code-wise, but I would like to just understand if it is possible if we do not know the depth.

JavaScript

Advertisement

Answer

You can simplify the loop by moving your base case out of it, which in turn makes it easier to see how to turn it into a comprehension:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement