Search code examples
pythontinydb

Get all the keys from TinyDB database


I create a tinydb database that looks as such:

{"_default": 
    {"1": {"word": "cat", "definition": "Its an animal."}, 
     "2": {"word": "cat", "definition": "Its an animal."}, 
     "3": {"word": "man", "definition": "has a hand"}, 
     "4": {"word": "girl", "definition": "Its a kid"}, 
     "5": {"word": "superman", "definition": "A hero."}
    }
}

I want to retrieve every value for the field "word". How is that possible?


Solution

  • You may retrieve all rows, then parse to get the different word field in a set for example

    all_rows = db.all()
    
    all_words = {row['word'] for row in all_rows}     # set comprehension
    all_words = set(map(lambda x: x['home'], values)) # map operation