This is Key!


#1

Hello,

I am not sure of the logic regarding the result being printed for the following code:

webster = {"aardvark : "a star of a popular children's cartoon show.",
"Baa" : "the sound a goat makes."
"Carpet" : "goes on the floor."
"Dab" : "a small amount."

for definitions in webster:
print webster[definitions]

How does this only print the values?


#2

Consider:

object = {
    'numbers': [1,2,3,4,5]
    'letters': ['a','b','c','d','e']
}
for key in object:
    print "%s: %r" % (key, object[key])
#                             value
numbers: [1, 2, 3, 4, 5]
letters: ['a', 'b', 'c', 'd', 'e']

#3

Hi mtf,

Thank you for your information, but I am still green with programming. Any further explanation?


#4

    letters: ['a', 'b', 'c', 'd', 'e']
#     key             value
# { 'letters':   object['letters']   }

Think of a dictionary as an associative array. Each item in the array has an arbitrary string index and a value or reference.


#5

That seems straightforward. I am still trying to figure out what commands are a certain pattern and others that that have some theory in how they work.

Thank you for your assistance!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.