Common Questions
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. |
|
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 |
|
|
Python cannot tell out of the box which character set your script uses, so you have to tell it. |
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. |
|
In Python 2, |
Integer division (in Python 2.x) can cause issues for those unaware that |
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 |
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 |
Sorting a list of version strings, eg |
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 |
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 |
It’s a hardware issue. |
The way Python handles newlines on Windows can result in blank lines appearing between rows when using |
- 1 - 30 of 30
- 1