# AttributeError: 'list' object has no attribute 'values'

**URL:** <https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479>\
**Category:** Uncategorized\
**Created:** [September 15, 2022, 6:21pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479 "2022-09-15T18:21:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AP\_M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ap_m/32/32203_2.png) [@AP\_M](https://discuss.pytorch.org/u/AP_M)\
**Post date:** [September 15, 2022, 6:21pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/1 "2022-09-15T18:21:58Z")

</div>

When I run the code, getting the error as:

```auto
AttributeError: 'list' object has no attribute 'values'

```

```auto
AttributeError Traceback (most recent call last)
<ipython-input-37-66115a52c5d6> in <module>
     23 optimizer.zero_grad()
     24 
---> 25 output = model(X_tr)
     26 optimizer.zero_grad()
     27 loss = loss_func(output)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887 result = self._slow_forward(*input, **kwargs)
    888 else:
--> 889 result = self.forward(*input, **kwargs)
    890 for hook in itertools.chain(
    891 _global_forward_hooks.values(),

<ipython-input-34-4a7275625fa6> in forward(self, x)
     13 
     14 def forward(self,x):
---> 15 h = self.f(x)
     16 return self.g(h)
     17 

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887 result = self._slow_forward(*input, **kwargs)
    888 else:
--> 889 result = self.forward(*input, **kwargs)
    890 for hook in itertools.chain(
    891 _global_forward_hooks.values(),

<ipython-input-32-ae2fe8e8ca2d> in forward(self, x)
     34 x = self.fc(x[:, -1, :])
     35 # out.size() --> 100, 10
---> 36 x = self.hybrid(x)
     37 return T.cat((x, 1 - x), -1)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887 result = self._slow_forward(*input, **kwargs)
    888 else:
--> 889 result = self.forward(*input, **kwargs)
    890 for hook in itertools.chain(
    891 _global_forward_hooks.values(),

<ipython-input-6-f66b87fe8d6c> in forward(self, input)
     42 
     43 def forward(self, input):
---> 44 return HybridFunction.apply(input, self.quantum_circuit, self.shift)

<ipython-input-6-f66b87fe8d6c> in forward(ctx, input, quantum_circuit, shift)
      8 ctx.quantum_circuit = quantum_circuit
      9 
---> 10 expectation_z = ctx.quantum_circuit.run(input[0].tolist())
     11 result = torch.tensor([expectation_z])
     12 ctx.save_for_backward(input, result)

<ipython-input-4-34122f68ae18> in run(self, thetas)
     30 result = job.result().get_counts()
     31 
---> 32 counts = np.array(list(result.values()))
     33 states = np.array(list(result.keys())).astype(float)
     34 

AttributeError: 'list' object has no attribute 'values'

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 15, 2022, 8:19pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/2 "2022-09-15T20:19:21Z")

</div>

The error seems to be raised in `ctx.quantum_circuit.run` which seems to be another library.  
Since `result.values()` and `result.keys()` are expected, I would assume this method expects `results` to be a `dict` and not a `list`.

---

<div class="post-metadata">

**Author:** ![AP\_M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ap_m/32/32203_2.png) [@AP\_M](https://discuss.pytorch.org/u/AP_M)\
**Post date:** [September 16, 2022, 7:38am UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/3 "2022-09-16T07:38:20Z")

</div>

The output of result is below which already has key value pairs.

```auto
RESULT [{'1': 49, '0': 51}, {'1': 58, '0': 42}, {'1': 48, '0': 52}, {'0': 53, '1': 47}, {'0': 51, '1': 49}, {'0': 51, '1': 49}, {'1': 58, '0': 42}, {'0': 47, '1': 53}, {'1': 54, '0': 46}, {'0': 47, '1': 53}, {'0': 41, '1': 59}, {'0': 52, '1': 48}, {'1': 47, '0': 53}, {'1': 52, '0': 48}, {'0': 52, '1': 48}, {'1': 50, '0': 50}, {'0': 41, '1': 59}, {'1': 54, '0': 46}, {'1': 55, '0': 45}, {'1': 44, '0': 56}, {'1': 61, '0': 39}, {'1': 53, '0': 47}, {'0': 40, '1': 60}, {'1': 59, '0': 41}, {'0': 60, '1': 40}, {'0': 56, '1': 44}, {'1': 46, '0': 54}, {'0': 46, '1': 54}, {'0': 53, '1': 47}, {'1': 52, '0': 48}, {'0': 49, '1': 51}, {'1': 55, '0': 45}, {'1': 51, '0': 49}, {'1': 48, '0': 52}, {'0': 53, '1': 47}, {'0': 56, '1': 44}, {'1': 53, '0': 47}, {'0': 51, '1': 49}, {'0': 45, '1': 55}, {'1': 47, '0': 53}, {'1': 55, '0': 45}]

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 16, 2022, 5:00pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/4 "2022-09-16T17:00:46Z")

</div>

That’s not entirely true, since your output shows a `list` containing `dict`s, which will still fail if you call `.values()` or `keys()` on it.

---

<div class="post-metadata">

**Author:** ![AP\_M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ap_m/32/32203_2.png) [@AP\_M](https://discuss.pytorch.org/u/AP_M)\
**Post date:** [September 16, 2022, 5:06pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/5 "2022-09-16T17:06:02Z")

</div>

According to this error, how should I make the changes in my code?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 16, 2022, 5:06pm UTC](https://discuss.pytorch.org/t/attributeerror-list-object-has-no-attribute-values/161479/6 "2022-09-16T17:06:36Z")

</div>

Make sure to pass a `dict` to this method, not a `list`.
