Skip to main content
Filter by
Sorted by
Tagged with
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 ...
Alex. S.'s user avatar
  • 146k
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 ...
Devoted's user avatar
  • 182k
7992 votes
31 answers
3.0m views

Does Python have a ternary conditional operator?

Is there a ternary conditional operator in Python?
Devoted's user avatar
  • 182k
7432 votes
26 answers
1.2m views

What are metaclasses in Python?

What are metaclasses? What are they used for?
Bite code's user avatar
  • 593k
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?
spence91's user avatar
  • 79.6k
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 ...
Carl Meyer's user avatar
  • 126k
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?
freshWoWer's user avatar
  • 63.8k
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.
Parand's user avatar
  • 106k
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 = ...
Joan Venge's user avatar
  • 328k
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 ...
Emma's user avatar
  • 54.8k
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?
Daryl Spitzer's user avatar
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 ...
Simon's user avatar
  • 80.4k
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?
Eugene M's user avatar
  • 48.7k
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 ...
TopChef's user avatar
  • 45.3k
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 ...
Roman's user avatar
  • 130k

15 30 50 per page
1
2 3 4 5
147278