-2

I am going through the "Head First" Learn to Program text for python 2.7, and as I was working through one of the exercises. It gave me a clue to multiple assign a line of data while splitting it. And since I could not guess it, I went to the solution, and I do not understand the solution.

I am running the latest python in the native IDLE

line = "101;Johnny 'wave-boy' Jones;USA;8.32;Fish;21"

display = {}

(display['ID'], display['Name'], display['Country'], display['Average'], display['Board'], display['Age']) = line.split(';')

I do not understand what is going on with the multiple assignments in the code above. How is the splitting making key and value pairs?

What role are the braces playing in this?

When I guessed for multiple assignments I imagined this:

(ID, Name, Country, Average, Board, Age) = line.split(;)

for a, b, c, d, e, f in line:
    display[ID] = Name, Country, Average, Board, Age

I thought that I could assign several values to one key, and just index or iterate the has with the keys() or items() method.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402

3 Answers3

1

I am also going through the 'Head First Programming' book and I also didn't understand the multiple assignment technique so I went through the solution and I understood it. Let me explain it to you:

A hash data structure keeps the data in the key: value pair. It is called dictionary in Python language and is created using the curly braces - {} If you read the section before the exercise it is given that when assigning a key: value to hash we use this code-

scores = {}
scores["joseph"] = 8.45

we put the key inside the square brackets of the scores variable(which is a hash data structure) and on the right hand side we put the value which is 8.45. The same mechanism is used in the multiple assignment technique.

We know that the line.split(";") method will give us 6 values so we put 6 keys[multiple assignment] on the left hand-side using the mechanism in the above code

(display['ID'], display['Name'], display['Country'], 
display['Average'],display['Board'], display['Age']) = line.split(';') 
 

and then the 6 values on the right hand side go to their respective key: value pairs

line.split(';') # gives 6 values 

REMEMBER: When you want to assign key: value pairs then you put the key in the square brackets inside the scores variable(hash data structure)scores['Joseph'] = 8.45... but when you have to refer to the value associated to the key you write this:

print(scores[key]) # this gives you the value in the key: value pair
print(ID) # this gives you the key in the key: value pair
Prithvvi
  • 11
  • 2
0

You could assign the values if there are in a list. This would work for instance:

for ID, Name, Country, Average, Board, Age in lines:
    display[ID] = [ Name, Country, Average, Board, Age ]
Nakor
  • 1,484
  • 2
  • 13
  • 23
0

The braces are a dictionary. This dictionary is called display. The line is divided up by the semicolon into parts, and these parts are in turn assigned as values to a key in the display dictionary. For example, this first assignment is 101.

display = {}
display['ID'] = '101`     # first part of `line`

In the code snippet in your question, could be simplified as something like the following:

display['first'], display['second'] =  ['A', 'B']

Here is a list of values (A and B) is each being assigned to keys (first and second) in the display dictionary. Note: you can not assign several "things" to one key; rather KEY and VALUE are a pair. You may iterate over a collection of "things" assigning values to various keys.

gregory
  • 10,969
  • 2
  • 30
  • 42