Python Forum
nested for loops to recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loops to recursion
#1
Any advice on how to convert nested for loops to recursion. I'm kinda having trouble since I'm still a beginner.

The loops are for finding all the possible words in a Boggle game (Twisted Word search).
Reply
#2
here's some sample recursive code, it's a method I use to display the contents of nested dictionaries
In this code, level is use to determine indentation level. line 32 calculates level * 4 spaces to determine how to indent
line 33 checks to see if value is a dictionary itself, and if so re curses using value as the dictionary
In this way, the dictionary tree can be walked.
food = {
    'Fruits and Fruit Juices': {
        'Acerola, (west indian cherry), raw': {
            'Nutrients': {
                'water': {
                    'unit': 'g',
                    'qty': '91.41'
                },
                'energy': {
                    'unit': 'kcal',
                    'qty': '32'
                }
            }
        },
    },
    'Figs, raw': {
        'Nutrients': {
            'water': {
                'unit': 'g',
                'qty': '91.41'
            },
            'energy': {
                'unit': 'kcal',
                'qty': '32'
            }
        }
    }
}

def display_dict(thedict, level=0):
    for key, value in thedict.items():
        sp = ' ' * (4 * level)
        if isinstance(value, dict):
            print(f'{sp}{key}:')
            level += 1
            display_dict(value, level)
        else:
            print(f'{sp}{key}: {value}')
            level -= 1

if __name__ == '__main__':
    display_dict(food)
results:
Output:
Fruits and Fruit Juices: Acerola, (west indian cherry), raw: Nutrients: water: unit: g qty: 91.41 energy: unit: kcal qty: 32 Figs, raw: Nutrients: water: unit: g qty: 91.41 energy: unit: kcal qty: 32
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,764 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,642 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,243 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,264 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,330 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,202 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Using recursion instead of for loops / list comprehension Drone4four 4 3,073 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,333 Jun-24-2020, 04:05 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020