-4

For this project, i need to know the name of the first key in the dictionary without knowing the actual name of the key. This is what I have so far:

import random
maxed_brawlers = {}
non_maxed_brawlers = {"Shelly" : 0}
coins = 0
gems = 0
max_powerpoints = 1410
powerpoint_levels = {"Level two" : 20, "Level three" : 30, "Level four" : 50, "Level five" : 
80, "Level six" : 130, "Level seven" : 210, "Level eight" : 340, "Level nine" : 550}
coin_levels = {"Level two" : 20, "Level three" : 35, "Level four" : 75, "Level five" : 140, 
"Level six" : 290, "Level seven" : 480, "Level eight" : 800, "Level nine" : 1250}
chat = input("Message... ")
while chat != "":
  for ele in non_maxed_brawlers:
    if non_maxed_brawlers[ele] == max_powerpoints:
      maxed_brawlers.append(ele)
      non_maxed_brawlers.pop(ele)
  if chat == "!box":
    if len(non_maxed_brawlers) == 1:
      random.shuffle(non_maxed_brawlers)
      amount = random.randint(10,15)
      for key in non_maxed_brawlers[0]:
        print(str(key) + str(amount))

I'm trying to print out the randomly generated number next to which key is getting it, but I won't be able to know the name of the key. I would only be needing the first and second one, so I would be able to use the index [0, 1]. Thank you in advance

ApexChikin
  • 37
  • 7
  • Look into `__dict__` – TomServo Aug 10 '21 at 22:37
  • 5
    `list(dictionary)[0]` will return the first key of the dictionary. – Barmar Aug 10 '21 at 22:40
  • Please be nice. This person is new to SO. @ApexChikin please go over https://stackoverflow.com/help/how-to-ask and revise your question based on this guide. You should isolate the part of the code you're confused by, not paste your entire assignment – Samantha Aug 10 '21 at 22:43
  • Since you're asking how to get the name of the key of a dictionary, maybe write out a simpler example of what you want to do instead of pasting this code. – Samantha Aug 10 '21 at 22:44
  • ``list(powerpoint_levels.keys())[0]`` – Karina Aug 10 '21 at 22:44
  • Conceptually, a `dict` is not ordered. Asking for "the first" key doesn't make sense, and the only reason it can be answered at all is so that `for` loops over the keys can work. You should have already noticed that `random.shuffle(non_maxed_brawlers)` does not work - `random.shuffle` expects a *sequence* that it can index into *with integers*, and assign to those indexes (in practical terms, this means you're going to use a `list`). It doesn't seem like there's any reason for `non_maxed_brawlers` to be anything other than a `list` in the first place, which resolves your issue. – Karl Knechtel Aug 10 '21 at 22:48
  • 2
    @KarlKnechtel Conceptually, dictionaries are ordered (in insertion order) since Python 3.7. – kaya3 Aug 10 '21 at 22:56
  • 1
    No, that is an implementation detail (even if they are guaranteeing it going forward) and not a part of the core concept. – Karl Knechtel Aug 10 '21 at 23:01
  • Does this answer your question? [How do you find the first key in a dictionary?](https://stackoverflow.com/questions/30362391/how-do-you-find-the-first-key-in-a-dictionary) – Gino Mempin Aug 10 '21 at 23:58

2 Answers2

1

Dictionaries iterate in insertion order. If you want the first key, make it an iterator and get its next value

>>> d = {"A":1, "B":2, "C":3}
>>> next(iter(d))
'A'
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

You can convert the dictionary keys and/or values to a list [please note powerpoint_levels is used as defined in the code provided]

    keys = list(powerpoint_levels.keys())  
    values = list(powerpoint_levels.values()) 
    print(keys[0]) #first key 
    print(values[0]) #first value

To get the second key or second value, just switch the 0's for 1's.