*** ValueError: not enough values to unpack (expected 2, got 1)

I’m trying to compare two dicts:

> x_state
[{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}]
> objects
[{'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}, {'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}]

To find differences easier I sort them first and then run some compare-methods over it. As most of the similar problems on SO result in a difference in the length or issues in reading of the file (x_state is a JSON from a file) I decided to count them:

unos=[sorted(l, key=itemgetter("ID"))for l in (x_state,objects)]
> unos
[[{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}], 
 [{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}]]
#Looks like nothing weird happened

> len(unos)
2
> len(unos[0])
4
> len(unos[1])
4

Seems fine, yet from that point everything just fails:

> for i,j in unos: print(f"{i}------{j}")
*** ValueError: too many values to unpack (expected 2)
> for i,j in zip(unos): print(f"{i}------{j}")
*** ValueError: not enough values to unpack (expected 2, got 1)

Trying something else to see whether or not both contain the same data:

> any(x != y for x, y in unos)
*** ValueError: too many values to unpack (expected 2)

What is going on here? And how would I properly debug this issue?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The sort command was executed wrongly. Correct:

ipdb> !for x,y in zip(sorted(x_state, key=itemgetter("ID")),sorted(objects, key=itemgetter("ID"))): print(x,y)
{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'} {'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}
{'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'} {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}
{'ID': 4, 'Name': 'D'} {'ID': 4, 'Name': 'D'}
{'ID': 5, 'Name': 'X'} {'ID': 5, 'Name': 'X'}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x