2,207,744 questions
13127
votes
51
answers
3.5m
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 ...
8433
votes
40
answers
5.0m
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 ...
8122
votes
32
answers
3.1m
views
Does Python have a ternary conditional operator?
Is there a ternary conditional operator in Python?
7525
votes
26
answers
1.3m
views
What are metaclasses in Python?
What are metaclasses? What are they used for?
7341
votes
41
answers
6.1m
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?
7120
votes
45
answers
3.7m
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 ...
6286
votes
66
answers
5.2m
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?
5814
votes
28
answers
4.0m
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.
5670
votes
28
answers
5.0m
views
How can I 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 = ...
5506
votes
34
answers
4.7m
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 ...
4789
votes
36
answers
1.2m
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?
4705
votes
38
answers
3.3m
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 ...
4487
votes
47
answers
6.7m
views
How can I 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?
4424
votes
17
answers
6.2m
views
Iterating over a dictionary using a 'for' loop, getting keys
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 ...
4249
votes
35
answers
8.1m
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 ...