Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a dictionary recursively in Python

Here is a dictionary:

data = {
    'a': {
        'b': {
            'c': {
                'd': {
                    'e': {
                        'f': 1,
                        'g': 50,
                        'h': [1, 2, 4],
                        'i': 3,
                        'j': [7, 9, 6],
                        'k': [
                            [('x', 'abc')],
                            [('y', 'qwe')],
                            [('z', 'zxc')]
                        ]
                    }
                }
            }
        }
    }
}

My goal is to find and transform values into dictionaries if possible:

data = {
    'a': {
        'b': {
            'c': {
                'd': {
                    'e': {
                        'f': 1,
                        'g': 50,
                        'h': [1, 2, 4],
                        'i': 3,
                        'j': [7, 9, 6],
                        'k': [{
                            'x': 'abc'
                        }, {
                            'y': 'qwe'
                        }, {
                            'z': 'zxc'
                        }]
                    }
                }
            }
        }
    }
}

I think that could be done using recursion and I have even written one, but it doesn't work.

def f(d):
  for key, value in d.iteritems():
    if type(d[key]) is dict:
      f(d)

    try:
      d[key] = dict(d[key])
    except:
      if type(d[key]) is list:
        for i in d[key]:
          try:
            d[key][i] = dict(d[key][i])
          except:
            pass

  return d

The error:

RecursionError: maximum recursion depth exceeded while calling a Python object

How do I make it work?

If you could provide a solution without recursion I'll be glad to get it as well.

like image 581
bay Avatar asked Oct 17 '22 06:10

bay


1 Answers

There are a bunch of errors in your program, lets go over them and come up with a working version.

def f(d):
  for key, value in d.iteritems():
    if type(d[key]) is dict:
      f(d)                        # You should call d[key] instead 
    try:
      d[key] = dict(d[key])       # Never assign an object back to the one you are iterating over, create a new object instead.
    except:
      if type(d[key]) is list:
        for i in d[key]:
          try:
            d[key][i] = dict(d[key][i])  # This doesn't work, can't convert a tuple/list this way.
          except:
            pass

  return d

Here is the corrected version of your code with two recursive functions. One for the lists and the other for the dictionaries.

def f1(value):
  e = []
  for val in value:
    if type(val) is list:
      e += f1(val)               # Append list to current list
    elif type(val) is tuple:
      e.append({val[0]: val[1]}) # Convert tuple to dictionary
    else:
      e.append(val)              # Append normal list values normally
  return e

def f(d, e  = {}):
  for key, value in d.iteritems():
    if type(value) is dict:
      e[key] = f(value, {})     # Recurse for dictionaries
    elif type(value) is list:
      e[key] = f1(value)        # Call the other recursive function for list
    else:
      e[key] = value            # Otherwise like strings and ints just append
  return e

Test it here: https://repl.it/LDKn/0

like image 105
TheChetan Avatar answered Oct 21 '22 00:10

TheChetan