In what order does Python read dictionary keys?

tcpeter at my-deja.com tcpeter at my-deja.com
Tue Jun 8 14:37:26 EDT 1999


Hello, all.  I've checked the docs and queried DejaNews for this but I
couldn't come up with an answer.  I wrote a script to convert rgb
values to hex for when I'm doing colors in HTML pages and don't have
Photoshop handy.  The code appears below.  My question is, how does
Python decide in which order to read the keys?  Notice that I have the
dictionary set up in this order: red, blue, green.  (I mistyped it while
writing the code).  But when I ran the code, the options were presented
in the desired order. (A trifle creepy, if you ask me.  Turing would be
delighted.)  Anyway, how does Python decide the order. Thanks.


#! /usr/bin/python

# The first thing to do is create a dictionary containing the
# questions to be asked where the color name is the dictionary key.

entries = {'red':'Type the red value, please: ',
	'blue':'Type the blue value, please: ',
	'green':'Type the green value, please: '}

for key in entries.keys():		# Now get a loop going
					# through the dictionaries

	foo = input(entries[key])	# Create a variable to hold
					# the return from the prompt
					# for each key

	if (foo) < 16:			# Since hex numbers don't
					# have leading zeroes below
					# F, I added them.

		print 'The hex equivalent for',foo,'is 0%X' % foo
	else:
		print 'The hex equivalent for',foo,'is %X' % foo


Tim Peter
(Not him, a different guy)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list