0

I have some code:

dict = {i:j for i in range(1,10,2) for j in range(10,2,-2)}

print(dict)

The output that I get is: {1: 4, 3: 4, 5: 4, 7: 4, 9: 4}

The output that I am expecting is: {1:10,3:8,5:6,7:4,9:2}

While I agree with the keys, what is happening to the values? Why are they stuck at 4? Because when I do range(10,2,-2) this produces 10,8,6,4,2

I have also tried:

dict= {}

for i in range(1,10,2):
    for j in range(10,2,-2):
        dict[i] = j

print(dict)

But yield the same result. Any ideas why? It must be my range() function.

  • it seems to have something to do with your second for loop's "step" – The shape Jun 19 '21 at 15:06
  • 1
    Note: This has nothing to do with your question. Please don't use ```dict``` as a variable name. – Ram Jun 19 '21 at 15:09
  • yea don't use dict as a variable name because `dict` is a `type` – The shape Jun 19 '21 at 15:09
  • 1
    Also ```range(10,2,-2)``` only gives ```10,8,6,4``` and not ```10,8,6,4,2``` as you've stated. ```range(start, end)``` only includes the ```start``` value and excludes the ```end``` value. – Ram Jun 19 '21 at 15:29
  • 1
    What exactly are you expecting? Perhaps you want `{i: j for i, j in zip(range(1,10,2), range(10, 2, -2)}` instead. – chepner Jun 19 '21 at 15:54
  • Please provide a [mre], meaning, add your expected output. You can [edit]. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Jun 19 '21 at 15:58
  • Thanks for the comments, I have edited the question to include the desired result, any ideas how to obtain this? – patrick chong Jun 20 '21 at 06:59

3 Answers3

3

Notice how you have two nested for loops:

dicti= {}

for i in range(1,10,2):
    for j in range(10,2,-2):
        dicti[i] = j

print(dicti)

The second for loop will loop through all values in for j in range(10,2,-2): And since your dicti[i] gets re-assigned to the newest value, your i's value will always be 4.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
The shape
  • 359
  • 3
  • 10
2

The values are all 4 because for each key in the outer loop, all the values in the inner loop are assigned, overwriting each time until the final value, which is 4.

You can get the result you want by zipping two ranges together.

>>> xp = {1:10,3:8,5:6,7:4,9:2}
>>> expected = {1:10,3:8,5:6,7:4,9:2}
>>> d = dict(zip(range(1, 10, 2), range(10, 0, -2)))
>>> expected == d
True

Note that the descending range goes from 10 to 0: 10 to 2 will not generate enough elements:

>>> list(range(10, 2, -2))
[10, 8, 6, 4]
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • Thanks for the solution snake. One question, I do understand zip(), but I fail to see how "d = dict(zip(range(1, 10, 2), range(10, 0, -2)))" is creating key/value pairs in the dictionary. Because normally we would have something like d[key] = value. what does dict(argument) do? Because I tried to run it on my IDE and can't seem to figure it out – patrick chong Jun 20 '21 at 08:04
  • Or is dict(zip(...)) a well known thing? – patrick chong Jun 20 '21 at 08:05
  • `dict` will accept a sequence of key-value pairs and create a dictionary: `dict([('a', 1), ('b', 2)])` -> `{'a': 1, 'b': 2}`. Using `zip` lets us create the key-value pairs from two sequences. It's quite a common idiom - see [this Q&A](https://stackoverflow.com/q/209840/5320906) for more examples and techniques. – snakecharmerb Jun 20 '21 at 08:11
1

You have to nested for loops, but only need one loop with pairs of values.

The next problem is, that the stop value in range is exclusive

{i:j for i, j in zip(range(1,10,2), range(10,1,-2))}
Daniel
  • 42,087
  • 4
  • 55
  • 81