4

I am writing some and I need to pass a complicated data structure to some function.

The data structure goes like this:

{ 'animals': [ 'cows', 'moose', { 'properties': [ 9, 26 ] } ]
  'fruits': {
    'land': [ 'strawberries', 'other berries' ],
    'space': [ 'apples', 'cherries' ]
  }
}

This structure looks pretty ugly to me. Can you think of ways how to simplify writing such massive data structures?

PS. I made up this structure but my real structure is very similar tho.

3
  • What's wrong with it? A dictionary of lists and dictionaries has no real problem with it. What are you concerned? What's wrong with it? Commented Feb 28, 2010 at 21:46
  • you have to describe your program in more detail. Are the properties [9, 26] the properties of the animals? Commented Feb 28, 2010 at 21:51
  • ` [ 'cows', 'moose', { 'properties': [ 9, 26 ] } ]` is undesireable. Lists gain power when all the elements share the same type (or at least the same duck-typable properties). Commented Feb 28, 2010 at 21:51

2 Answers 2

5

Other languages would solve this problem with objects or structs-- so, something like:

class whatever:
    animals = AnimalObject()
    fruits = FruitObject()

class AnimalObject:
    animals = ['cows','moose']
    properties = [9,26]

class FruitObject:
    land = ['strawberries', 'other berries']
    space = ['apples', 'cherries']

Of course, this only works if you know ahead of time what form the data is going to take. If you don't, then maps/lists are your only choice ;-)

Sign up to request clarification or add additional context in comments.

Comments

2
  1. Use objects. You are working with basic types like strings and dictionaries while objects are more powerful.
  2. Use function arguments. You can pass the the first-level keys in your dictionary as arguments to your function:
def yourfunction(animals, fruits)
    # do things with fruits and animals
    pass

2 Comments

Or he can leave his structure as it is and pass it as yourfunction(*his_variable)
@voyager less expressive in my opinion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.