How do you Store Data in a Dictionary?


hello, I have multiple song details (title, lyrics, link, etc.), and I want to store them in a dictionary using song_code as the key. Each song_code should have multiple titles, lyrics, and links stored in a list.

For example, I have the following data:

song_code: AG001
song_title: Amazing Grace
song_lyric: Amazing grace! how sweet the sound...
play_link: https://play.example.com/ag001

Later, I might add another title and lyric , playlink for the same song_code, so the structure should look like this in a dictionary:

{
"AG001": {
"titles": ["Amazing Grace", "Amazing Grace (Cover by John)"],
"lyrics": ["Amazing grace! how sweet the sound...", "Amazing grace! how sweet the sound... (cover version)"],
"play_link": ["https://play.example.com/ag001", "https://play.example.com/ag001-cover"]
}
}

How can I store this structure in a dictionary and save it in TinyDB ? I also want to display all song titles in a ListView.Thanks in advance!

better to be like this:

{
	"ag001":[
		{
			title:a title,
			lyrics:amazing grace,
			play_link: some real link
		},
		{
			title:title2,
			lyrics:amazing grace2,
			play_link: some real link2
		},
		...
	],
	"ag002":[
		{
			title:a title,
			lyrics:amazing grace,
			play_link: some real link
		},
		{
			title:title2,
			lyrics:amazing grace2,
			play_link: some real link2
		},
		...
	]
}
1 Like

How to do that?

A TinyDB namespace is like a persistent dictionary.

Try defining a namespace for every key/value combination.

That would create a column based data base.

You might need four or five of them, and nonunique names or titles might need to store lists.

1 Like


SOMETHING LIKE THIS?

1 Like

If the combination title/code is unique, you could use that as a TinyDB tag (text JOIN of the two parts with a '/' or '|' between them and either store the remaining attributes under that tag as a dictionary, or use separate Namespaces for each attribute (link, lyrics,...) with the same compound title/code tag.

I like using this TinyDb Taglist to feed a ListView. If you want to get fancy, split the tags into major and detail text parts of your ListView Elements in a loop over your taglist.

1 Like

I prefer to save all data under one tag like this:

1 Like

thank you, it worked

Thank you for your help

thanks for the help, yes that's right. sorry for late reply

no prob, but both of them diff approach. anyhow your doubts was cleared and learned something with dict, that's ok.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.