Questions tagged [python]
Python is a dynamically typed, multi-purpose programming language. It is designed to be quick to learn, understand, and use, and enforces a clean and uniform syntax. Note that Python 2 reached end-of-life on January 1st, 2020. For version-specific Python questions, add the version tag (e.g. [python-3.x] or [python-3.9]). When using a Python variant (e.g. Jython, PyPy) or library (e.g. Pandas, NumPy), please include it in the tags.
python
202,339
questions
246
votes
5
answers
55k
views
How to make good reproducible pandas examples
Having spent a decent amount of time watching both the r and pandas tags on SO, the impression that I get is that pandas questions are less likely to contain reproducible data. This is something that ...
556
votes
18
answers
269k
views
How do I create variable variables?
I know that some other languages, such as PHP, support a concept of "variable variable names" - that is, the contents of a string can be used as part of a variable name.
I heard that this is ...
760
votes
22
answers
1.1m
views
Asking the user for input until they give a valid response
I am writing a program that accepts user input.
#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18:
...
849
votes
31
answers
569k
views
How to test multiple variables for equality against a single value?
I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say:
x = 0
y ...
957
votes
19
answers
80k
views
List of lists changes reflected across sublists unexpectedly
I created a list of lists:
>>> xs = [[1] * 4] * 3
>>> print(xs)
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
Then, I changed one of the innermost values:
>>> xs[0][0] = 5
>...
4611
votes
38
answers
3.1m
views
How slicing in Python works
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice?
See Why are slice and range upper-bound ...
629
votes
5
answers
75k
views
How can I pivot a dataframe? [closed]
What is pivot?
How do I pivot?
Long format to wide format?
I've seen a lot of questions that ask about pivot tables, even if they don't know it. It is virtually impossible to write a canonical ...
931
votes
8
answers
438k
views
Pandas Merging 101
How can I perform a (INNER| (LEFT|RIGHT|FULL) OUTER) JOIN with pandas?
How do I add NaNs for missing rows after a merge?
How do I get rid of NaNs after merging?
Can I merge on the index?
How do I ...
3444
votes
34
answers
273k
views
The Mutable Default Argument in Python
Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue:
def foo(a=[]):
a.append(5)
return a
Python novices would expect this function called with ...
55
votes
6
answers
91k
views
I'm getting an IndentationError (or a TabError). How do I fix it?
I have a Python script:
if True:
if False:
print('foo')
print('bar')
However, when I attempt to run my script, Python raises an IndentationError:
File "script.py", line 4
...
3316
votes
24
answers
2.2m
views
How do I clone a list so that it doesn't change unexpectedly after assignment?
While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it? For example:
>>> my_list = [1, 2, 3]
...
259
votes
10
answers
1.1m
views
How can I read inputs as numbers?
Why are x and y strings instead of ints in the below code?
(Note: in Python 2.x use raw_input(). In Python 3.x use input(). raw_input() was renamed to input() in Python 3.x)
play = True
while play:
...
931
votes
25
answers
935k
views
How to remove items from a list while iterating?
I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria.
for tup in somelist:
if determine(tup):
code_to_remove_tup
What should I ...
5379
votes
34
answers
4.4m
views
How do I make a flat list out of a list of lists?
I have a list of lists like
[
[1, 2, 3],
[4, 5, 6],
[7],
[8, 9]
]
How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?
If your list of lists comes from a nested list ...
136
votes
8
answers
244k
views
Selenium - wait until element is present, visible and interactable
I have a Selenium script (Python) that clicks a reply button to make the class anonemail appear. The time it takes for the class anonemail to appear varies. Because of that I have to use sleep until ...