victorhooi
victorhooi

Reputation: 17275

Python - Nested dict, versus hashing a tuple?

I'm wondering what are the pros/cons of nested dicts versus hashing a tuple in Python?

The context is a small script to assign "workshops" to attendees at a conference.

Each workshop has several attributes (e.g. week, day, subject etc.). We use four of these attributes in order to assign workshops to each attendee - i.e. each delegate will also have a week/day/subject etc.

Currently, I'm using a nested dictionary to store my workshops:

workshops = defaultdict(lambda: defaultdict(lambda:defaultdict(lambda:defaultdict(dict))))

with open(input_filename) as input_file:
    workshop_reader = csv.DictReader(input_file, dialect='excel')

    for row in workshop_reader:
        workshops[row['Week ']][row['Stream']][row['Strand']][row['Day']] = row

return workshops

I can then use the above data-structure to assign each attendee their workshop.

The issue is later, I need to iterate through every workshop, and assign an ID (this ID is stored in a different system), which necessitates unwrapping the structure layer by layer.

First question - is there some other way of creating a secondary index to the same values, using a string (workshop name) as a key? I.e. I'd still have the four-level nested dicts, but I could also search for individual entries based on just a name.

Secondly - how can I achieve a similar effect using tuples as the key? Are there any advantages you can think of using this approach? Would it be much cleaner, or easier to use? (This whole unwrapping this is a bit of a pain, and I don't think it's very approachable).

Or are there any other data structures that you could recommend that might be superior/easier to access/manipulate?

Thanks, Victor

Upvotes: 1

Views: 1707

Answers (2)

Lennart Regebro
Lennart Regebro

Reputation: 172249

Generally, if you need to nest dictionaries, you should use classes instead. Nested dictionaries gets complicated to keep track of when you are debugging. With classes you can tell different types of dictionaries apart, so it gets easier. :)

Upvotes: 4

aeter
aeter

Reputation: 12700

class Workshop(object):
  def __init__(self, week, stream, strand, day, name):
    self.week = week
    self.stream = stream
    self.day = day
    self.strand = strand
    self.name = name

...
for row in workshop_reader:
  workshops['name'] = Workshop(...)

That is only if the name is the unique attribute of the workshops (that is, no workshops with repeating names).
Also, you'll be able to easily assign an ID to each Workshop object in the workshops dictionary.

Upvotes: 7

Related Questions