I am trying to create a dictionary and assign a value of 0 to each value
Here’s what I have so far:
c.execute("""SELECT category from wordtest order by category""")
catnum = c.fetchall()
catnum = [list(x) for x in catnum]
catnum = sum(catnum, [])
catnums = defaultdict(list)
for key in catnum:
catnums[key].append(0)
But the output I get for this is {"key": [0]} and I do not want the value in a list.
Category in the database is just a list of words.
How would I make it so that the value of catnums, per key, is 0?
Am I missing something? Doesn’t the following do what you want?:
Why do you need a defaultdict here?
Note that if you want a defaultdict which defaults to having values that default to 0, you can do:
since
int()returns0.