Python - How to fix “ValueError: not enough values to unpack (expected 2, got 1)”
How you should debug your code
'''
@param d: a dictionary
@param key_value_pairs: a list of tuples in the form `(key, value)`
@return: a list of tuples of key-value-pair updated in the original dictionary
'''
def add_to_dict(d, key_value_pairs):
newlist = []
for pair in key_value_pairs:
# As is mentioned by Mr Patrick
# you might not want to unpack the key-value-pair instantly
# to avoid possible corrupted data input from
# argument `key_value_pairs`
# if you can't guarantee its integrity
try:
x, y = pair
except (ValueError):
# unable to unpack tuple
tuple_length = len(pair)
raise RuntimeError('''Invalid argument `key_value_pairs`!
Corrupted key-value-pair has ({}) length!'''.format(tuple_length))
# Instead of using nesting loop
# using API would be much more preferable
v = d.get(x)
# Check if the key is already in the dictionary `d`
if v:
# You probably mean to append a tuple
# as `array.append(x)` takes only one argument
# @see: https://docs.python.org/3.7/library/array.html#array.array.append
#
# Besides, hereby I quote
# "The function should return a list of all of the key/value pairs which have changed (with their original values)."
# Thus instead of using the following line:
#
# newlist.append((x, y,))
#
# You might want a tuple of (key, old_value, new_value)
# Hence:
newlist.append((x, v, y,))
# I don't know if you want to update the key-value-pair in the dictionary `d`
# take out the following line if you don't want it
d[x] = y
return newlist
Please keep reading the remaining part if you want to know how to traverse a dict
object properly.
Different ways to traverse a dict
object
Python 3.x
The following segments demonstrate how to traverse a dict
in Python 3.x.
Iterate the set of keys
for key in d:
value = d[key]
print(key, value)
the code segment above has the same effect as the following one:
for key in d.keys():
value = d[key]
print(key, value)
Iterate the set of key-value-pairs
for key, value in d.items():
print(key, value)
Iterate the set of values
for value in d.values():
print(value)
Python 2.x
The following segments demonstrate how to traverse a dict
in Python 2.x.
Iterate the set of keys
for key in d:
value = d[key]
print(key, value)
keys()
returns a list of the key set of dictionary d
for key in d.keys():
value = d[key]
print(key, value)
iterkeys()
returns an iterator of the key set of dictionary d
for key in d.iterkeys():
value = d[key]
print(key, value)
Iterate the set of key-value-pairs
values()
returns a list of the key-value-pair set of dictionary d
for key, value in d.items():
print(key, value)
itervalues()
returns an iterator of the key-value-pair set of dictionary d
for key, value in d.iteritems():
print(key, value)
Iterate the set of values
values()
returns a list of the value set of dictionary d
for value in d.values():
print(value)
itervalues()
returns a iterator of the value set of dictionary d
for value in d.itervalues():
print(value)
Reference:
- What is the difference between list and iterator in Python?
ValueError: not enough values to unpack (expected 2, got 1) when splitting strings
When you assign two variables like this:
code, add = name.split('-')
Python expects the right-hand part to contain two values. That's why it said:
not enough values to unpack (expected 2)
It expected two values. You probably tried to split a filename that had no -
symbol, because Python received only one value after the split. That's why it said:
(expected 2, got 1)
ValueError: not enough values to unpack (expected 2, got 1) how can i solve this problem i am just tried
You have a trailing comma on this line
STATICFILES_DIRS = [STATIC_DIR],
^
It makes STATICFILES_DIRS a list of tuples instead of a list of strings.
Delete that comma. Pls, let me know if it helps.
Also perhaps this might help as well
ValueError: not enough values to unpack (expected 2, got 1) using list
This is because you have a list of lists of tuples, not a list of tuples. So you'd need something like:
for item in namelist:
for name, newname in item:
# do something
(or change your data as written in the comment above)
Related Topics
How to Locate the Input Within Div
How to Calculate a Gaussian Kernel Matrix Efficiently in Numpy
How to Remove Lowest Elements in List
Arrange a Text File Using Python
How to Count Duplicate Rows in Pandas Dataframe
Pandas Join Dataframes Based on Conditions
Expression to Remove Url Links from Twitter Tweet
Write a Program That Find the Largest Integer in a String
Check If a Python Script Is Already Running in Windows
Insert Comma into Text File Using Python
How to Insert Text At Line and Column Position in a File
Running Command With Paramiko Exec_Command Causes Process to Sleep Before Finishing
How to Append New Data Onto a New Line
Count Duplicates Between 2 Lists
How to Extract Rar Files Inside Google Colab
Run a Python Script from Another Python Script, Passing in Arguments