Building a list inside a list in python

You could append empty lists until you have enough to access the index you have data for: However, you may want to use a dictionary instead, letting you implement a sparse object instead. A collections.defaultdict() object is especially useful here: data now has keys 2 and 5, each a list with one element. No entries … Read more

Random word generator- Python

Reading a local word list If you’re doing this repeatedly, I would download it locally and pull from the local file. *nix users can use /usr/share/dict/words. Example: Pulling from a remote dictionary If you want to pull from a remote dictionary, here are a couple of ways. The requests library makes this really easy (you’ll … Read more

Inserting image into IPython notebook markdown

Files inside the notebook dir are available under a “files/” url. So if it’s in the base path, it would be <img src=”files/image.png”>, and subdirs etc. are also available: <img src=”files/subdir/image.png”>, etc. Update: starting with IPython 2.0, the files/ prefix is no longer needed (cf. release notes). So now the solution <img src=”image.png”> simply works … Read more

Mutable strings in Python

Do you know of a Python library which provides mutable strings? Google returned surprisingly few results. The only usable library I found is http://code.google.com/p/gapbuffer/ which is in C but I would prefer it to be written in pure Python. Edit: Thanks for the responses but I’m after an efficient library. That is, ”.join(list) might work … Read more

Can’t install Scipy through pip

After opening up an issue with the SciPy team, we found that you need to upgrade pip with: And in Python 3 this works: for SciPy to install properly. Why? Because: Older versions of pip have to be told to use wheels, IIRC with –use-wheel. Or you can upgrade pip itself, then it should pick … Read more