0

So I have a dictionary which I have generated with an expression, My goal is to compare class values and return the name of the object which meets a certain condition I just thought that it will be easy to accomplish by creating a dictionary, I did it like this:

class A:
    def __init__(self, name: str, value: int):
        self.name = name
        self.value = value

variants = 3
variants = dict({'v' + str(num): num for num in range(1, variants + 1, 1)})

p.s. I'm doing it this way because it's a part of a much more complex code.

In the given class the objects are:

v1 = A('v1', 201)
v2 = A('v2', 200)
v3 = A('v3', 2) 

but now if I'm trying to access the values of the variants dict all it does is it's splitting the key instead of delivering it next to the value:

for key, value in variants:
print(key, value)
      #out
v 1
v 2
v 3

If i'm trying to print each entry:

for x in variants:
print(x)
    #out
v1
v2
v3

while If i'm trying to print out the whole dictionary, it works...

print(variants)
    #out
{'v1': 1, 'v2': 2, 'v3': 3}

Any help would be appreciated! Thanks!

balderman
  • 22,927
  • 7
  • 34
  • 52
Tomer Poliakov
  • 349
  • 1
  • 3
  • 12

0 Answers0