Python Forum

Full Version: Dictionary in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm learning French and Python, and I thought I'd combine the two. My goal is to make a glossary written in Python, where each word is a dictionary with three keys (word category, article and chapter in which it first appears) and values. I'd like to store those words in a list; this list is for nouns. So far I've come up with this (indentation is screwed up, but originally correct):

voyager_glossary_noun = []

voyage = {
    'category': 'noun',
    'article': 'le',
    'chapter': '1a',
    }

voyager_glossary_noun.append(voyage)

quai = {
    'category': 'noun',
    'article': 'le',
    'chapter': '1a',
    }

voyager_glossary_noun.append(quai)
I have trouble accessing the various data. I'd like to have an output like this:

quai:
* noun
* le
* chapter 1a

I've written this:

1. for word in voyager_glossary_noun:
2.     
3.     for key, value in voyage.items():
4.         msg = f"\t* {value}"
5.         print(msg)
I don't understand how to display the word itself, and I get the impression that I should be able to do that with one line of code on the second line. Any tips? I'm an almost total beginner, and simple code and explanation would be greatly appreciated.
(Dec-27-2023, 01:30 PM)bashage Wrote: [ -> ]I don't understand how to display the word itself,
The word itself would be a string, and it is saved nowhere.
voyage = {...} is a dictionary, a variable name with no reference to the string' voyage'.
That is why I always try to apply the so called "Hungarian notation", where you prefix the name of a
variable with it's type.
So dictVoyage = {...} or lstVoyage = [] or setVoyage = (...)
You will need to repeat Voyage as an element of the dict, altough there might be better ways to do this.
Paul
I would make a class for the words. It will save a ton of typing later.
from dataclasses import dataclass


@dataclass
class Word:
    category: str
    article: str
    chapter: str


glossary = {"voyage": Word("noun", "le", "la"), "quai": Word("noun", "le", "la")}

for word, entry in glossary.items():
    print(word, entry)
Output:
voyage Word(category='noun', article='le', chapter='la') quai Word(category='noun', article='le', chapter='la')
I might make the glossary a class too.
from dataclasses import dataclass


@dataclass
class Word:
    category: str
    article: str
    chapter: str


class Glossary:
    def __init__(self):
        self.words = {}

    def noun(self, word: str, article: str, chapter: str):
        self.words[word] = Word("noun", article, chapter)
        return self

    def verb(self, word: str, article: str, chapter: str):
        self.words[word] = Word("verb", article, chapter)
        return self

    @property
    def nouns(self):
        return {
            word: entry
            for word, entry in self.words.items()
            if entry.category == "noun"
        }

    @property
    def verbs(self):
        return {
            word: entry
            for word, entry in self.words.items()
            if entry.category == "verb"
        }

    def __getitem__(self, word):
        return self.words[word]

    def __iter__(self):
        return iter(self.words)


glossary = Glossary()
glossary.noun("voyage", "le", "la")
glossary.noun("quai", "le", "la")

for word in glossary.nouns:
    print(word, glossary[word])

print("Lookup voyage", glossary["voyage"])
Output:
voyage Word(category='noun', article='le', chapter='la') quai Word(category='noun', article='le', chapter='la') Lookup voyage Word(category='noun', article='le', chapter='la')