1

I want to create a dictionary in python with 40 key/value pairs. They keys should be a1, a2 ... a10, b1, b2 ... b10, c1, c2 ... c10, d1, d2 ... d10. I have a for loop that takes two variables and modifies them so they change after each loop. It loops 40 times to create 40 different values for each of the two variables.

In the dictionary, the value for the key a1, should be the value of x and y in the first loop. The value for the key a2 should be the value of x and y in the second loop. This continues to the key d10 which should have the value of x and y in the 40th loop.

How can I do this? I think I can use a for loop, but since I struggle with understanding for loops, I don't know what I should do.

Here is my code:

x = 200
y = 202

for i in range(40):
    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84

I want a dictionary that looks like this:

dictionary = {'a1':(200, 202), 'a2':(# the value of x and y after looping a second time, here it will be 200, 286)}

0

3 Answers 3

1
letters = "abcd"
dictionary = {}

x = 200
y = 202

for i in range(40):
    key = letters[int(i / 10)] + str(i % 10 + 1)
    dictionary[key] = x, y

    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84
Sign up to request clarification or add additional context in comments.

5 Comments

Traceback (most recent call last): File "/path/path/path/file.py", line 13, in <module> key = letters[int(i / 10 - 1)] + i % 10 TypeError: must be str, not int
@greenGrass Please try the new version.
It seems like it worked very well. I'll check to see if it did exactly what I wanted.
I will also see a little bit on the other answer to see if I can learn something
It looks like this did exactly what I needed.
0

Here's an elegant solution that keeps track of both the letter and the number, and formats them into the key using fstring.

x = 200
y = 202

# Create the dict first
res = {}

# Assign the letter to begin with
key_letter = 'a'

for i in range(0, 40):
    if i != 0 and i % 10 == 0:
        # Increment the letter since 10th element reached
        key_letter = chr(ord(key_letter) + 1)
    # Format the letter with the number
    res[f'{key_letter}{i % 10 + 1}'] = (x, y)
    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84
print(res)

Output:

{'a1': (200, 202), 'a2': (200, 286), 'a3': (200, 370), 'a4': (200, 454), 'a5': (200, 538), 'a6': (200, 622), 'a7': (200, 706), 'a8': (200, 790), 'a9': (200, 874), 'a10': (200, 958), 'b1': (300, 202), 'b2': (300, 286), 'b3': (300, 370), 'b4': (300, 454), 'b5': (300, 538), 'b6': (300, 622), 'b7': (300, 706), 'b8': (300, 790), 'b9': (300, 874), 'b10': (300, 958), 'c1': (400, 202), 'c2': (400, 286), 'c3': (400, 370), 'c4': (400, 454), 'c5': (400, 538), 'c6': (400, 622), 'c7': (400, 706), 'c8': (400, 790), 'c9': (400, 874), 'c10': (400, 958), 'd1': (500, 202), 'd2': (500, 286), 'd3': (500, 370), 'd4': (500, 454), 'd5': (500, 538), 'd6': (500, 622), 'd7': (500, 706), 'd8': (500, 790), 'd9': (500, 874), 'd10': (500, 958)}

This way you don't have to maintain a separate string to keep track of all the letters.

Comments

0

The following is solely based on that the 40 iterations come from a-d x 1-10, so I got rid of your for-loop.

x = 200
y = 202
your_dict = {}

for j in ["a","b","c","d"]:
    for k in range(1,11):
        key = j + str(k)
        your_dict[key] = (x,y)
        if y != 202 + 9 * 84:
            y += 84
        else:
            x += 100
            y = y - 9 * 84

Result:

{'a1': (200, 202), 'a2': (200, 286), 'a3': (200, 370), 'a4': (200, 454), 'a5': (200, 538), 'a6': (200, 622), 'a7': (200, 706), 'a8': (200, 790), 'a9': (200, 874), 'a10': (200, 958), 'b1': (300, 202), 'b2': (300, 286), 'b3': (300, 370), 'b4': (300, 454), 'b5': (300, 538), 'b6': (300, 622), 'b7': (300, 706), 'b8': (300, 790), 'b9': (300, 874), 'b10': (300, 958), 'c1': (400, 202), 'c2': (400, 286), 'c3': (400, 370), 'c4': (400, 454), 'c5': (400, 538), 'c6': (400, 622), 'c7': (400, 706), 'c8': (400, 790), 'c9': (400, 874), 'c10': (400, 958), 'd1': (500, 202), 'd2': (500, 286), 'd3': (500, 370), 'd4': (500, 454), 'd5': (500, 538), 'd6': (500, 622), 'd7': (500, 706), 'd8': (500, 790), 'd9': (500, 874), 'd10': (500, 958)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.