Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How i get the value from a dictionary if the key from all keys are the same?

I wanna from a dictionary all values but i have for each value the same key. It is possible that i can get the values in a separately empty list?

dic = QuesModel.objects.values("ans").distinct()
print(dic) # i get: """<QuerySet [{‘ans’: ‘antwort1’}, {‘ans’: ‘answer2’}, {‘ans’: ‘besitzt als Modelle
elemente verschiedene Ereignistypen.’}, {‘ans’: ‘als Nachrichten und Datenobjekte
e dargestellt.’}, {‘ans’: ‘2 ist eine rationale Zahl’}, {‘ans’: ‘x hat den Wert 5
5’}, {‘ans’: ”}]>
"""

and i wanna get the values from dic in a list, Like:
for key, values in dic.items(): # but this doesn `t work
How can i fixed that??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes you can do the following:

dic1 = list(QuesModel.objects.values("ans").distinct())
answers = [i['ans'] for i in dic1]
print(answers)

Note: Models in Django don’t require model to be the suffix, so it is better to name it as Ques only or Question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading