Fix Python – Create a list with initial capacity in Python

Code like this often happens:
l = []
while foo:
# baz
# 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 – Print list without brackets in a single row

I have a list in Python
names = [“Sam”, “Peter”, “James”, “Julian”, “Ann”]

I want to print the array in a single line without the normal ” []
names = [“Sam”, “Peter”, “James”, “Julian”, “Ann”]
print (names)

Will give the output as;
[“Sam”, “Peter”, “James”, “Julian”, “Ann”]

That is not the format I want instead I want it to be like this;

Fix Python – Removing a list of characters in string

I want to remove characters in a string in python:
string.replace(‘,’, ”).replace(“!”, ”).replace(“:”, ”).replace(“;”, ”)…

But I have many characters I have to remove. I thought about a list
list = [‘,’, ‘!’, ‘.’, ‘;’…]

But how can I use the list to replace the characters in the string?

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 – Find the most common element in a list

What is an efficient way to find the most common element in a Python list?
My list items may not be hashable so can’t use a dictionary.
Also in case of draws the item with the lowest index should be returned. Example:
>>> most_common([‘duck’, ‘duck’, ‘goose’])
>>> most_common([‘goose’, ‘duck’, ‘duck’, ‘goose’])