0

I have this list with dictionaries:

[{"Name": "Tore", "Score": 7}, {"Name": "Arne", "Score": 10}, {"Name": "Terje", "Score": 4}]

and want to put the information in variables so i can get this:

first = "Tore
first_number = 7
second = "Arne
second-number = 10

How do I access these values in these variables?

Tried allot of different ways, but neither works.

Tore
  • 99
  • 10
  • Honestly the loose variables "first" and "second" etc are much less useful. You can't iterate over them. Why not use the far more idiomatic `lst[0]`, `lst[1]`, `lst[n]` to get the "first", "second" "n-th" item and then access the property via a string index into the dict? If your list contains 1000 elements do you really want a variable like `nine_hundred_ninety_ninth_number`? Related elements should be collected in a data structure exactly as you have them originally. – ggorlen Nov 10 '20 at 16:09
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables). In particular, read [this answer](https://stackoverflow.com/a/38972761/6243352) carefully. – ggorlen Nov 10 '20 at 16:10
  • Hi. Thanks for the comment. It answers my question. I rewrote some of the code so now I have three variables like this referencing the sorted content in my json file: player_one = str("1: " + sorted_player_rank[0]["Name"] + " Score: " + str(sorted_player_rank[0]["Score"])) – Tore Nov 13 '20 at 09:09
  • I made three variables because im only showing top three scores in my game. This works, but im sure it could be done in a more elegant way. – Tore Nov 13 '20 at 09:12
  • If you share what you're trying to accomplish (show the string output you want to achieve, for example) there may be a better way. – ggorlen Nov 13 '20 at 15:51

0 Answers0