Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: dictionary update sequence element #0 has length 3; 2 is required

I am trying to insert values in MongoDB, but I am getting this error:

ValueError: dictionary update sequence element #0 has length 3; 2 is required.

From pymongo import MongoClient

client = MongoClient()

db = client.abc_database

keys = []
values = []
key = input("enter keys:").split(",")
keys.append(key)
print(keys)
print(keys[0][1])
value = input("enter values").split(",")
values.append(value)
print(values)
fin = []
k = 0
while k < len(keys[0]):
    d = keys[0][k] + ":" + values[0][k]
    fin.append(d)
    k += 1
print(fin)
fin_id = db.fin.insert(dict([fin]))
print("successful")
like image 433
Nazaf Anwar Avatar asked Jan 23 '26 19:01

Nazaf Anwar


1 Answers

You are trying construct a dict from key-value pairs by first concatenating each key to its value:

d = keys[0][k] + ":" + values[0][k]

Make the key-value pairs tuples instead:

d = keys[0][k], values[0][k]

Then you should be able to use dict(fin). Note that fin is already a list; don't wrap in another list like you do in dict([fin]).

like image 89
Janne Karila Avatar answered Jan 26 '26 12:01

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!