-2

I am a python rookie and so my question is simple (yet I couldn't find the answer here):

I need to access values in my dictionary (named 'database') but without knowing the actual values. So lets say I want to print the first value of the dictionary whatever it is. I found this:

print(database.values()[0].keys()[0])

Which seems to be what I'm looking for but when running the script I get this error:

TypeError: 'database' object does not support indexing

Can you please help?

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Jan Pisl
  • 1,043
  • 2
  • 14
  • 29
  • 2
    There is no such thing as "the first value" because dictionaries are unordered. You can get the first thing in `values` but there's no guarantee it will be the first later. Anyway, if you want to do that, why are you involving `keys()` at all? Just do `database.values()[0]`. – BrenBarn Apr 09 '17 at 01:37
  • I have given sample code below that works. Check it out – prashanth manohar Apr 09 '17 at 01:57
  • I guess I didnt explain it very well. So, I have a dictionary called database that stores student IDs as key and student names as values. I want the user to input grades for each student for a number of exams (the number of exams is same for all students). And the output should look like this: {'1234': {'Name': 'Josh', 'Scores': [88,99,77]}}. So I need to loop through the dictionary and change {'1234': Josh} to {'1234': {'Name': 'Josh', 'Scores': [88,99,77]}} but I dont know why. – Jan Pisl Apr 09 '17 at 02:32
  • This question you asked now is very far from the title and the description you have given. You may want to open a new thread by asking the question separately with proper description. – prashanth manohar Apr 09 '17 at 03:03

1 Answers1

0

You might want to check out Ordered Dict:

As others mentioned in the comments, you would get the elements in no particular order because dictionaries are unordered.

This sample code works:

from collections import OrderedDict

database = OrderedDict()
database = {
          "key1": {"key10": "value10", "key11": "value11"},
          "key2": {"key20": "value20", "key21": "value21"}
          }

If you want to print out or access all first dictionary keys in database.values() list, you may use something like this:

for key, value in database.items():
    print value.keys()[0]

If you want to just access first key of the first item in database.values(), this may be useful:

print database.values()[0].keys()[0]

Hope this helps.

prashanth manohar
  • 531
  • 1
  • 13
  • 30
  • Thank you but that isnt it exactly the same as I wrote (the last line of code)? Anyway, I guess I didnt explain it very well. So, I have a dictionary called database that stores student IDs as key and student names as values. I want the user to input grades for each student for a number of exams (the number of exams is same for all students). And the output should look like this: {'1234': {'Name': 'Josh', 'Scores': [88,99,77]}}. But I dont know how to assign each student list of his grades so it looks like Im suggesting above. – Jan Pisl Apr 09 '17 at 02:26