2,209,161
questions
12982
votes
52
answers
3.4m
views
What does the "yield" keyword do in Python?
What functionality does the yield keyword in Python provide?
For example, I'm trying to understand this code1:
def _get_child_candidates(self, distance, min_dist, max_dist):
if self._leftchild and ...
8300
votes
40
answers
4.7m
views
What does if __name__ == "__main__": do?
What does this do, and why should one include the if statement?
if __name__ == "__main__":
print("Hello, World!")
If you are trying to close a question where someone should be ...
7992
votes
31
answers
3.0m
views
Does Python have a ternary conditional operator?
Is there a ternary conditional operator in Python?
7432
votes
26
answers
1.2m
views
What are metaclasses in Python?
What are metaclasses? What are they used for?
7230
votes
41
answers
5.7m
views
How do I check whether a file exists without exceptions?
How do I check whether a file exists or not, without using the try statement?
7017
votes
43
answers
3.4m
views
How do I merge two dictionaries in a single expression in Python?
I want to merge two dictionaries into a new dictionary.
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = merge(x, y)
>>> z
{'a': 1, 'b': 3, 'c': 4}
Whenever a key k is present in both ...
6203
votes
66
answers
4.9m
views
How do I execute a program or call a system command?
How do I call an external command within Python as if I had typed it in a shell or command prompt?
5726
votes
27
answers
3.8m
views
How do I create a directory, and any missing parent directories?
How do I create a directory at a given path, and also create any missing parent directories along that path? For example, the Bash command mkdir -p /path/to/nested/directory does this.
5532
votes
27
answers
4.7m
views
How to access the index value in a 'for' loop?
How do I access the index while iterating over a sequence with a for loop?
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
Desired output:
item #1 = 8
item #2 = ...
5401
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 ...
4708
votes
36
answers
1.1m
views
What is the difference between @staticmethod and @classmethod in Python?
What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
4632
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 ...
4430
votes
46
answers
6.5m
views
How to find the index for a given item in a list?
Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1?
4353
votes
17
answers
5.9m
views
Iterating over dictionaries using 'for' loops
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
print(key, 'corresponds to', d[key])
How does Python recognize that it needs only to read the key from the dictionary? Is key a special keyword, or is ...
4160
votes
34
answers
7.6m
views
How can I iterate over rows in a Pandas DataFrame?
I have a pandas dataframe, df:
c1 c2
0 10 100
1 11 110
2 12 120
How do I iterate over the rows of this dataframe? For every row, I want to access its elements (values in cells) by the name ...