When should I use uuid.uuid1() vs. uuid.uuid4() in python?

uuid1() is guaranteed to not produce any collisions (under the assumption you do not create too many of them at the same time). I wouldn’t use it if it’s important that there’s no connection between the uuid and the computer, as the mac address gets used to make it unique across computers. You can create duplicates by creating … Read more

TypeError: ‘int’ object is not callable

Somewhere else in your code you have something that looks like this: Then when you write that is interpreted as meaning a function call on the object bound to round, which is an int. And that fails. The problem is whatever code binds an int to the name round. Find that and remove it.

How can I install pip on Windows?

Python 2.7.9+ and 3.4+ Good news! Python 3.4 (released March 2014) and Python 2.7.9 (released December 2014) ship with Pip. This is the best feature of any Python release. It makes the community’s wealth of libraries accessible to everyone. Newbies are no longer excluded from using community libraries by the prohibitive difficulty of setup. In shipping with a package … Read more

Removing duplicates in lists

The common approach to get a unique collection of items is to use a set. Sets are unordered collections of distinct objects. To create a set from any iterable, you can simply pass it to the built-in set() function. If you later need a real list again, you can similarly pass the set to the list() function. The following example should cover whatever you … Read more

How do I install pip on macOS or OS X?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. I spent most of the day yesterday searching for a clear answer for installing pip (package manager for Python). I can’t find a good solution. How do I install it?

Print string to text file

It is strongly advised to use a context manager. As an advantage, it is made sure the file is always closed, no matter what: This is the explicit version (but always remember, the context manager version from above should be preferred): If you’re using Python2.6 or higher, it’s preferred to use str.format() For python2.7 and higher … Read more

How to make a flat list out of a list of lists

Given a list of lists t, which means: is faster than the shortcuts posted so far. (t is the list to flatten.) Here is the corresponding function: As evidence, you can use the timeit module in the standard library: Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(T**2) when there are T sublists — as the intermediate … Read more