list() uses slightly more memory than list comprehension

I think you’re seeing over-allocation patterns this is a sample from the source: /* This over-allocates proportional to the list size, making room * for additional growth. The over-allocation is mild, but is * enough to give linear-time amortized behavior over a long * sequence of appends() in the presence of a poorly-performing * system … Read more

Alternative to list comprehension if there will be only one result

You could use a generator expression and next instead. This would be more efficient as well, since an intermediate list is not created and iteration can stop once a match has been found: actor = next(actor for actor in self.actors if actor.name==actorName) And as senderle points out, another advantage to this approach is that you … Read more

Why is a list comprehension so much faster than appending to a list?

List comprehension is basically just a “syntactic sugar” for the regular for loop. In this case the reason that it performs better is because it doesn’t need to load the append attribute of the list and call it as a function at each iteration. In other words and in general, list comprehensions perform faster because … Read more

Flattening a list of NumPy arrays?

You could use numpy.concatenate, which as the name suggests, basically concatenates all the elements of such an input list into a single NumPy array, like so – import numpy as np out = np.concatenate(input_list).ravel() If you wish the final output to be a list, you can extend the solution, like so – out = np.concatenate(input_list).ravel().tolist() … Read more

What do backticks mean to the Python interpreter? Example: `num`

Backticks are a deprecated alias for repr(). Don’t use them any more; the syntax was removed in Python 3.0. Using backticks seems to be faster than using repr(num) or num.__repr__() in version 2.x. I guess it’s because additional dictionary lookup is required in the global namespace (for repr), or in the object’s namespace (for __repr__), … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)