Reputation: 169
I have a list that contains lists. I want to save it in firestore under a map that exists
list:
[['1', 'B'],
['5', 'E'],
['7', 'J']]
Output needed in firestore:
letter_count (map)
1:'B'
5:'E'
7:'J'
Any help?
Upvotes: 1
Views: 933
Reputation: 3768
Use update()
to add if the list is not existing in document and update the field if it is existing, for example:
UPDATE: convert into dictionary based on @Sabari's answer:
letter_case = {x[0]: x[1] for x in [['1', 'B'], ['5', 'E'], ['7', 'J']]}
doc_ref = db.collection(u'testcol').document(u'testdoc')
doc_ref.update({'letter_count': letter_case })
Upvotes: 0
Reputation: 4383
It is not possible to store lists within lists in firebase, unfortunately (more info on that here: https://stackoverflow.com/a/59068112/8612435). What you can do is convert that list into a dictionary and pass that data into firebase instead:
# convert to dictionary
mydict = {}
for i in range(len(mylist)):
mydict[mylist[i][0]] = mylist[i][1]
print(mydict)
# push to firebase
db.collection('collection-name').document('document-name').set({'letter_count': mydict})
(Note: I took inspiration in multidimensional list conversion to dictionary through this post: convert multidimensional list into dictionary python)
Upvotes: 3
Reputation: 41
Convert list into dictionary before you update it.
letter_case = {x[0]: x[1] for x in [['1', 'B'], ['5', 'E'], ['7', 'J']]}
print(letter_case)
Upvotes: 1