-1

I have a dictionary:

d = {'A': 1, 'B': 2, 'C':3}

I need the Key 'A' to always be first in this dict because certain processes throughout the program depend on it being there.

While performing operations on this dictionary it will get unordered.

What is the easiest to ensure 'A' remains the first key throughout this program no matter operations I perform on it?

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

-1

Make it an OrderedDict instead of a plain dictionary, add A as the first key before you do anything else, and never delete that key.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
-1

Dictionary objects don't guarantee to keep the keys and values in the order they were inserted, but Python 3.1 has a collections.OrderedDict class that can be used for this purpose.

Daniel Reyhanian
  • 579
  • 4
  • 26