0

In Python 3.x, I understand that by default, if we iterate over a dictionary, the loop variable is assigned the value of the key each iteration. As such:

for loopvar_1 in {'a':1,'b':2,'c':3}:
 print(loopvar_1)

results in

a
b
c

What benefit does it serve to assign the key to the loop variable instead of the actual values associated with that key (the numbers 1, 2 and 3 in this case.)

And so following from what I asked above, if we necessarily do need to refer to the key each time how can we iterate through the values without using any functions or methods?

user134505
  • 41
  • 8
  • 2
    The key is going to be unique and can be used to lookup the associated value. If it returned the values (which could all be the same) - you wouldn't be able to tie it back to the key... – Jon Clements Jul 26 '18 at 15:51
  • 1
    Please don't roll back legitimate edits - if you have multiple questions, ask them separately. – jonrsharpe Jul 26 '18 at 15:51
  • If you want the values, it is not hard to do: `for key, val in {'a':1, 'b':2, 'c':3}.items()` – Ben Jones Jul 26 '18 at 15:53
  • yeah, since that returns both the key and the associated value which in turn can be assigned – user134505 Jul 26 '18 at 16:00
  • @jonrsharpe I rolled back the edit so I could specifically avoid any responses that detailed any methods and functions, since I had expected this to happen -_- – user134505 Jul 26 '18 at 16:04

2 Answers2

2

This is discussed in PEP-234, which defined the behaviour of dictionary iterators (emphasis mine):

There has been a long discussion about whether

for x in dict: ...

should assign x the successive keys, values, or items of the dictionary. The symmetry between if x in y and for x in y suggests that it should iterate over keys. This symmetry has been observed by many independently and has even been used to "explain" one using the other. This is because for sequences, if x in y iterates over y comparing the iterated values to x. If we adopt both of the above proposals, this will also hold for dictionaries.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    Of course, that assumes you agree that `in` should test if `x` is a key, not a value. – chepner Jul 26 '18 at 15:54
  • 1
    @chepner presumably that was already the case at the time of the PEP. Also it's way more efficient to find a key than a value in a hash table. – jonrsharpe Jul 26 '18 at 15:57
0

One way to bring out all the associated values is to use the default method to refer to an associated value, and doing so inside a for loop so that the key used to call the associated value is changed each time.

mnm={'a':1,'b':2,'c':3}
for z in mnm:
    print(mnm[z])

PS:This was what I additionally wanted to know when asking the question, unfortunately the part about 'not using a method or function' was edited out as irrelevant.

user134505
  • 41
  • 8
  • There is a pretty big difference between asking "Why is python this way?" and asking "How can I do this in python?" – Ben Jones Jul 26 '18 at 19:41
  • Yes I won't deny, my bad . I just started and was having some trouble phrasing it. – user134505 Jul 26 '18 at 19:45
  • At this point I don't even know what I'm unaware of so my questions and the changes I make turn out to be somewhat absurd at times – user134505 Jul 26 '18 at 19:46