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.

A mutable object is modified in a loop and appended to a list. The OP is surprised that all of the list items are duplicates of the last appended object.

The asker does new_list = old_list or new_dict = old_dict and is surprised when changes to the one reference are reflected in the other. The fix is to copy the list or dict in one of many ways.

An IndexError is raised because the asker tried using lst[len(lst)] = new_val to append to a list. They should have used lst.append (and maybe lst.extend) instead.

Create a single executable (exe) from a script, package, or other project. A common question when using Windows.

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.

listdir() returns just the file names; you have to os.path.join() back the directory name

How can I extract different parts of a string or list using slicing notation or the slice() built-in?

Unpacking assignment from a list, tuple, map, etc. into separate names

Specifying the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDEs

TypeError: list indices must be integers or slices, not ...

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.

Python throws an error if you modify the dict you are currently iterating over. The obvious workaround is to copy the keys to a separate list, or collecting the keys to change into a separate list and modifying them separately after the loop.

“Astonishing” behaviour of default mutable arguments: def cabbage(leaves=[]).

A single string or other object, possibly in parentheses, is passed as the second argument to execute().

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

Flask provides the jsonify method to convert data into a JSON response.

Why does id({}) == id({}) and id([]) == id([]) in CPython?

Why do my variables disappear when I quit and restart?

What format should I use for saving?

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?

I have a list of n-many objects - why does list.index(0) raise a ValueError and not return the first item?

A typo like list["abcd"] instead of list("abcd") leads to Type Error: “'types.GenericAlias' object is not ...”

When using str.join a list comprehension (rather than a generator expression) provides a significant speed boost

The terminology makes finding a canonical “That’s a list / dict / etc. comprehension” question hard.

  • 1 - 30 of 30
  • 1