Get list of values at specific position in TinyDB

90 Views Asked by At

I have a TinyDB for Python and the data with multiple lines in the structure {'service': {'key1': '', 'key2': '', 'key3': '', 'key4': '', 'key5': ''}}

I wanted to get only "service" from all the lines of the database assuming the "service" real name is not known.

How to achieve with tinyDB Query()?

db.all()[0] just give entire line in index 0.

I am expecting to get only "service".

2

There are 2 best solutions below

0
Alderven On BEST ANSWER

It's a bit tricky:

>>> list(db.all()[0].keys())[0]
service

>>> list(db.all()[0].values())
[{'key1': '', 'key2': '', 'key3': '', 'key4': '', 'key5': ''}]
0
shaik moeed On

To get service from all the outputs,

def get_key_info(db_output, key):
   return [row[key] for row in db_output]

output = db.all()
get_key_info(output, 'service')

To just get the service first row,

db.get(doc_id=1)['service']