Python Forum
Dictonary and list help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictonary and list help
#4
You should restructure your dictionary similar to:
movie_dict = {
    '2006': {
        'The Prestige': {
            'director': 'Christopher Nolan',
            # then can add additional information about movie, like:
            'actors': {
                'stars': {
                    'Hugh Jackman': {
                        'role': 'Robert Angier - rival stage magician in London at the end of the 19th century',
                        '...': '...'
                    },
                    'Christian Bale': {
                        'role': 'Alfred Borden'
                    }
                }
            }
        },
        'The Departed': {
            'director': 'Martin Scorsese',
            'writer': 'William Monahan',
            'actors': {
                '...': '...'
            }
        }
    }
} 
'...' just indicates where you would add additional information

the way that you have it sort of defeats the purpose of having a dictionary in the first place.
If written like above, you can add and remove information very easily, and access any way you wish.

for sorting by value, refer to this post: https://python-forum.io/Thread-sorting-n...-to-values
and this article: https://www.blog.pythonlibrary.org/2012/...-by-value/

Additional - post edit
you can save a large dictionary as a JSON file, and read it in simply wherever needed. I usually create my dictionaries in a separate script, save as JSON and then read a=in as needed, here's code (haven't tested, but I think it's correct) to do that:
import json


def save_json(dictname, filename):
    with open(filename, 'w') as fp:
        json.dump(dictname, fp)

def load_json(dictname, filename):
    dictname = json.load(fp)
Reply


Messages In This Thread
Dictonary and list help - by Btoulouse - Dec-09-2018, 08:44 PM
RE: Dictonary and list help - by Larz60+ - Dec-09-2018, 09:52 PM
RE: Dictonary and list help - by Btoulouse - Dec-09-2018, 10:01 PM
RE: Dictonary and list help - by Larz60+ - Dec-10-2018, 10:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a substring in a dictonary; value substring of key aapurdel 2 7,080 May-31-2019, 06:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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