Fix Python – Create a list with initial capacity in Python

Code like this often happens:
l = []
while foo:
# baz
l.append(bar)
# qux

This is really slow if you’re about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements.
In Java, you can create an ArrayList with an initial capacity. If you have some idea how big your list will ….

Fix Python – How can I get dictionary key as variable directly in Python (not by searching from value)?

Sorry for this basic question but my searches on this are not turning up anything other than how to get a dictionary’s key based on its value which I would prefer not to use as I simply want the text/name of the key and am worried that searching by value may end up returning 2 or more keys if the dictionary has a lot of entries… what I am trying….

Fix Python – What’s a correct and good way to implement __hash__()?

What’s a correct and good way to implement __hash__()?
I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries.
As __hash__() returns an integer and is used for “binning” objects into hashtables I assume that the values of the returned integer should be uniformly distributed for ….

Fix Python – Object of custom type as dictionary key

What must I do to use my objects of a custom type as keys in a Python dictionary (where I don’t want the “object id” to act as the key) , e.g.
class MyThing:
def __init__(self,name,location,length):
self.name = name
self.location = location
self.length = length

I’d want to use MyThing’s as keys that are co….

Fix Python – Convert [key1,val1,key2,val2] to a dict?

Let’s say I have a list a in Python whose entries conveniently map to a dictionary. Each even element represents the key to the dictionary, and the following odd element is the value
for example,
a = [‘hello’,’world’,’1′,’2′]

and I’d like to convert it to a dictionary b, where
b[‘hello’] = ‘world’
b[‘1’] = ‘2’

What is the syntactically cleanest….

Fix Python – Is there a difference between using a dict literal and a dict constructor?

Using PyCharm, I noticed it offers to convert a dict literal:
d = {
‘one’: ‘1’,
‘two’: ‘2’,
}

into a dict constructor:
d = dict(one=’1′, two=’2′)

Do these different approaches differ in some significant way?
(While writing this question I noticed that using dict() it seems impossible to specify a numeric key .. d = {1: ‘one’, 2: ‘two’}….