2

I am writing a script in which the computer will play a game of war with itself. I need the script to only return the value of the first entry in the list:

Note: This is only an example

    test = {'hey':1, 'hi':2, 'hello':3, 'greetings':4, 'goodmorning':5,   'night':6}

    for a in test:
        print test[a]

    #this returns
    1
    2
    3
    4
    5
    6

I would like this script to only return the value of the first entry.

code_dredd
  • 5,915
  • 1
  • 25
  • 53
  • 3
    Dictionaries are not ordered collections in the sense that you don't access the *values* by position (i.e. 1st, 2nd, etc); you access them by keys (e.g. `test['hey']`, `test['hi']`, etc.) – code_dredd Oct 30 '15 at 20:37
  • did you try: `print a` as well? – RobertB Oct 30 '15 at 20:41
  • Do you mean to return [1] as per your example ? – H. U. Oct 30 '15 at 20:42
  • 1
    Possible duplicate of [Python dictionary, keep keys/values in same order as declared](http://stackoverflow.com/questions/1867861/python-dictionary-keep-keys-values-in-same-order-as-declared) – Jose Ricardo Bustos M. Oct 31 '15 at 17:17

2 Answers2

2

Since dictionaries are unordered, and if you are dependent on using a dictionary, but order matters to you, you should consider using an OrderedDict from collections. This will preserve the order of your dictionary based on insertion. So, if you enter a, v, b, it will be in that order.

Demo:

>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d['a'] = 1
>>> d['v'] = 1
>>> d['b'] = 1
>>> d
OrderedDict([('a', 1), ('v', 1), ('b', 1)])

To get the first value based on requirements to get only the first entry:

d.items()[0][1]
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • yes, but I need for the only thing to be returned to be the value of the entry. In this particular case, the value is 1. –  Oct 30 '15 at 20:50
  • @Mezex Yes...look at my answer...the last line of my example gets you that. – idjaw Oct 30 '15 at 20:50
  • @Mezex -You can simply append it to list like : list1.append(dict_name.values()[0]) – H. U. Oct 30 '15 at 20:51
  • @Mezex: try `d.items()[0][1]` for getting the first tuple in the dictionary and then the *value* instead of the *key*. – code_dredd Oct 30 '15 at 20:52
  • That's a good point @ray . Easier to follow. I can update my answer with that. Thanks. – idjaw Oct 30 '15 at 20:53
2

The built-in Dictionaries are not ordered collections in the sense that you don't access the values by indexed positions (i.e. 1st, 2nd, etc).

Instead, you access them by keys (e.g. test['hey'], test['hi'], etc.). For example:

>>> test = { 'hey':1, 'hi':2, 'hello':3 }
>>> test[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0
>>> test['hi']
2

You should look at the Python docs on using dictionaries.

If you want to access elements by position (i.e. an index), then you should consider using a list or a tuple instead.

You might want to look at the OrderedDict class docs instead if you wish to preserve order while still using a dictionary.

Quoting from the docs:

An OrderedDict is a dict that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

See idjaw's post for a good example on using the OrderedDict.

code_dredd
  • 5,915
  • 1
  • 25
  • 53