How to add an element to the beginning of an OrderedDict?

There’s no built-in method for doing this in Python 2. If you need this, you need to write a prepend() method/function that operates on the OrderedDict internals with O(1) complexity. For Python 3.2 and later, you should use the move_to_end method. The method accepts a last argument which indicates whether the element will be moved … Read more

ImportError: DLL load failed: %1 is not a valid Win32 application for Python Matplotlib

The error that you are getting is because you have installed the wrong component of matplotlib (there are the 32 bit and 64 bit components). This page provides you all binaries (32bit,64bit) for Windows. It also includes other packages apart from matplotlib if you may be needing them in the future. Try installing the proper … Read more

How can I scrape a page with dynamic content (created by JavaScript) in Python?

EDIT Sept 2021: phantomjs isn’t maintained any more, either EDIT 30/Dec/2017: This answer appears in top results of Google searches, so I decided to update it. The old answer is still at the end. dryscape isn’t maintained anymore and the library dryscape developers recommend is Python 2 only. I have found using Selenium’s python library … Read more

Fast string array – Cython

Try following code. to_cstring_array function in the following code is what you want. from libc.stdlib cimport malloc, free from libc.string cimport strcmp from cpython.string cimport PyString_AsString cdef char ** to_cstring_array(list_str): cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *)) for i in xrange(len(list_str)): ret[i] = PyString_AsString(list_str[i]) return ret def foo(list_str1, list_str2): cdef unsigned int i, … Read more

What arguments does Python sort() function have?

Arguments of sort and sorted Both sort and sorted have three keyword arguments: cmp, key and reverse. L.sort(cmp=None, key=None, reverse=False) — stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 sorted(iterable, cmp=None, key=None, reverse=False) –> new sorted list Using key and reverse is preferred, because they work much faster than an equivalent cmp. key … Read more