1

I have a dictionary with the name of a circle arc and the location for the arc. I would like to use for loop to create all the arcs in the dictionary. When I run this code I can error TypeError: 'dict_keys' object does not support indexing

Here is the relevant code:

self.gaugeDict={"gauge1": [100,105,100],
                                "gauge2": [300,105,100],
                                "gauge3": [100, 210, 100],
                                "gauge4": [300, 210, 100]}
        for i in range(len(self.gaugeDict)):
            self.w.create_circle_arc(self.gaugeDict.keys()[i][0],
                                     self.gaugeDict.keys()[i][1],
                                     self.gaugeDict.keys()[i][2],
                                     fill="black",
                                     outline="",
                                     start=180,
                                     end=0)
Mureinik
  • 252,575
  • 45
  • 248
  • 283

3 Answers3

1

Since , dict.keys() does not return a list of keys (that can be used like somedict.key()[i]), but a proxy object, to iterate over the keys.

But we can iterate over the keys (and values) concurrently. Based on your code, you are in fact probably not interested in the keys at all, but only in the values, we thus can iterate over the values like:

for a, b, c in self.gaugeDict.values():
    self.w.create_circle_arc(
        a,
        b,
        c,
        fill="black",
        outline="",
        start=180,
        end=0
)

We also make use here of a technique called iterable unpacking: every value in the dictionary, the list with three values, is unpacked in a, b and c. This will however fail if there are list with more elements. In that case we can however use a throwaway variable to catch the remaining elements:

for a, b, c, *__ in self.gaugeDict.values():
    self.w.create_circle_arc(
        a,
        b,
        c,
        fill="black",
        outline="",
        start=180,
        end=0
)

or we can use indexing:

for value in self.gaugeDict.values():
    self.w.create_circle_arc(
        value[0],
        value[1],
        value[2],
        fill="black",
        outline="",
        start=180,
        end=0
)
Willem Van Onsem
  • 321,217
  • 26
  • 295
  • 405
1

Seems like you just need to iterate the dict's values:

for x in self.gaugeDict.values():
    self.w.create_circle_arc(x[0],
                             x[1],
                             x[2],
                             fill="black",
                             outline="",
                             start=180,
                             end=0)
Mureinik
  • 252,575
  • 45
  • 248
  • 283
1

You could not do that as rightly pointed out by @Willem

See below interpreter example

>>> dict1={0:"abc",1:"ab",2:"a"}
>>> dict1.keys()
dict_keys([0, 1, 2])
>>> for key in dict1.keys():
...         print(key)
...
The output would be as follows  
0
1
2

But in python 2 it is possible thar you get list in return to your call to dictionary keys

>>> dict1={0:"New York", 1:"California", 2:"Bangalore"}
>>> dict1.keys()
[0, 1, 2]

So as suggested above make appropriate changes for your code to work

Naseer Mohammad
  • 344
  • 3
  • 12