Fix Python – How can I initialize a dictionary of distinct empty lists in Python?

My attempt to programmatically create a dictionary of lists is failing to allow me to individually address dictionary keys. Whenever I create the dictionary of lists and try to append to one key, all of them are updated. Here’s a very simple test case:
data = {}
data = data.fromkeys(range(2),[])
print data

Actual result: {….

Fix Python – Python: converting a list of dictionaries to json

I have a list of dictionaries, looking some thing like this:
list = [{‘id’: 123, ‘data’: ‘qwerty’, ‘indices’: [1,10]}, {‘id’: 345, ‘data’: ‘mnbvc’, ‘indices’: [2,11]}]

and so on. There may be more documents in the list. I need to convert these to one JSON document, that can be returned via bottle, and I cannot understand how to do this. Please he….

Fix Python – How to iterate through a list of dictionaries in Jinja template?

I tried:
list1 = [{“username”: “abhi”, “pass”: 2087}]
return render_template(“file_output.html”, list1=list1)

In the template:

{% for dictionary in list1 %}
{% for key in dictionary %}

Key Value

{{ key }}

Fix Python – Modifying a Python dict while iterating over it

Let’s say we have a Python dictionary d, and we’re iterating over it like so:
for k, v in d.iteritems():
del d[f(k)] # remove some item
d[g(k)] = v # add a new item

(f and g are just some black-box transformations.)
In other words, we try to add/remove items to d while iterating over it using iteritems.
Is this well defined? Could you pro….

Fix Python – Will OrderedDict become redundant in Python 3.7?

From the Python 3.7 changelog:

the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.

Would this mean that OrderedDict will become redundant? The only use I can think of it will be to maintain backwards compatibility with older versions of Python which don’t preserve insertio….

Fix Python – Only add to a dict if a condition is met

I am using urllib.urlencode to build web POST parameters, however there are a few values I only want to be added if a value other than None exists for them.
apple = ‘green’
orange = ‘orange’
params = urllib.urlencode({
‘apple’: apple,
‘orange’: orange

That works fine, however if I make the orange variable optional, how can I prevent it….