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")
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]).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With