This is a list of common questions within the Python tag. Each question includes some canonical SO questions that can be used to close-vote any new questions that match. If you have any suggestions then please come see us in chat.

  • Each space separated value is a separate search term.
  • Terms are ANDed together.
  • [Brackets] indicate a tag term.
  • is:draft shows only draft posts.
  • is:community shows only community posts.
  • 1 - 30 of 30
  • 1

Sometimes it is useful to access both the current and next item when iterating over a list.

Problems typically being solved by using a dispatch dict.

'12' < '2', and min(['12', '2']) == '12'

Given a byte string, determine what charset should be used to decode it to text.

Multiplying a list creates a shallow copy, not a deep copy, so any mutable objects in the list are then shared. Solution is to use a list comprehension to create separate objects.

Using open("relative/path") raises FileNotFoundError even though the file exists

Python cannot tell out of the box which character set your script uses, so you have to tell it.
The error you get is SyntaxError: Non-ASCII character '\xe2' in file bla.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Python 2 and 3 allow tabs to be used to indent lines. Python 2 will expand these to a multiple of 8, in Python 3 a tab can be equal only to another tab;, but many editors and Stack Overflow expand tabs to multiples of 4 instead. This can lead to subtle and weird flow errors.

input (Python 3.x) and raw_input (Python 2.x) return the variable as a string, this can confuse users when they expect their “number” to be a number.

In Python 2, input treats strings as variable names and raises a NameError.

Integer division (in Python 2.x) can cause issues for those unaware that 1/2 != 0.5

Zip can be used to iterate over multiple lists at the same time. In Python 3.x a generator is used whilst in Python 2.x a new list of tuples is created, as such itertools.izip may be a better choice.

A negative number raised to an even power returns a negative number due to the operator precedence.

Starting with Python 3.x print changed from a statement to a function.

How to output Value is "42", yet print 'Value is: "', 42, '"', or print('Value is: "', 42, '")' adds extra spaces around 42

Division can sometimes trip up the new user, especially the difference between Python 2.x and 3.x.

How to remove items from a list while iterating over the same list without skipping elements.

The result set of a single column query is a list of tuples, instead of a list
of values.

Sorting a list of version strings, eg ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"], into natural order.

How to split a single list into multiple lists with a fixed (maximum) size?

Adding a trailing comma can cause problems, primarily the assignment of a tuple rather than the intended object.

The splat operator (*) can be used to unpack variables from a container when calling a function.

If a function does not return a value then it automatically returns None.

Python parses 2. as the float 2.0, this can cause troubles when trying to use methods on integers.

Coming from other languages people might try to use && and || as logical operators, which fails. The obvious solution is to use & and | instead… and then it still “doesn’t work”, because they need and and or.

It’s a hardware issue.

The way Python handles newlines on Windows can result in blank lines appearing between rows when using csv.writer.

  • 1 - 30 of 30
  • 1