Iterate hash data with for

Let's add some additional rows of data to your hash:

scores[9.12] scores[7.21]

The new rows have been added

Once you have a hash created, you can use the trusty for loop to iterate over each of the rows:

scores

pata in a hash is maintained a seemingly random order, Ut don t worry about that -for now-

The new rows have been added pata in a hash is maintained a seemingly random order, Ut don t worry about that -for now-

The wkeys()" built-in method reW an arrayof Take each ^ x. the keys of the hash. There are no extra po'nts for

^ iftetST what the %alues()" method does.

for key in scores.keys():

print(scores[key] + ' had a score of ' + str(name_part)) ...and display a custom ^ essage using the data

eath row of the hash.

Here's what would display on screen — (assuming all the data in "results.txt" was available to the hash).

Another hash method, called items (), returns each key-value pair in turn, and can be used with the for loop, too:

-/he 'Y™0 method returns each key-value pair.

for score, surfer in scores.items():

Whichever method you use to iterate over your hash's data is up to you, because using items () or keys () produces the same output.

Code Magnets

Rearrange the code magnets at the bottom of the page to complete the program shown below. Rather than using two arrays, this program stores the results from the surfing contest in a hash:

result_f = open("results.txt") for line in result_f:

result_f.close()

print("The top scores were:")

I for eaeh_score in scores.keys()T~)

surfers with their scores

Code Magnets Solution

You were to rearrange the code magnets at the bottom of the page to complete the program shown below. Rather than using two arrays, this program stores the results from the surfing contest in a hash:

You need to start With a« e-.?ty hash, ^ as o??osed to a« empty array-

result_f = open("results.txt") for line in result_f:

\ After splitting out the name and the score, use scores [score] - name f «score" as the key of the hash and result f.close() the value of W asthe value-

You'll find out soon why the We"

Print("The top scores were:")

Use a for loop —^ -------case) as the key of the hash "

| print ( ' Surfer ' + |Scores[each_score] + ' scored_'j+_each_score) \ \ p

Display each row from the hash, describing the assoCiaW-

Dumb Questions

Q/ Can I append data to a hash just like I did with an array?

A" Yes, and no. There's no append () method for hashes like the one included with every array. To add a new row of data to an existing hash, use code similar to that used in the solution, above. Hashes do have their own set of methods, but append() is not one of them.

Can I use anything as the key of a hash?

A" No, you can't. The rules Python applies here can be complex (as they can be in other programming languages, too). The best advice we can give you is to stick with numbers and strings as keys. Trust us: this isn't as restrictive as it first sounds and is, by far, the best/easiest strategy to follow.

Was this article helpful?

0 0

Post a comment