Skip to main content
13127 votes
51 answers
3.5m views

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
  • 148k
8433 votes
40 answers
5.0m views

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
  • 185k
8122 votes
32 answers
3.1m views

Is there a ternary conditional operator in Python?
Devoted's user avatar
  • 185k
7525 votes
26 answers
1.3m views

What are metaclasses? What are they used for?
Bite code's user avatar
  • 601k
7341 votes
41 answers
6.1m views

How do I check whether a file exists or not, without using the try statement?
spence91's user avatar
  • 80.8k
7120 votes
45 answers
3.7m views

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
  • 128k
6286 votes
66 answers
5.2m views

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
  • 64.7k
5814 votes
28 answers
4.0m views

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
  • 107k
5670 votes
28 answers
5.0m views

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
  • 334k
5506 votes
34 answers
4.7m views

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
  • 55.9k
4789 votes
36 answers
1.2m views

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer's user avatar
4705 votes
38 answers
3.3m views

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
  • 81.3k
4487 votes
47 answers
6.7m views

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
  • 49.3k
4424 votes
17 answers
6.2m views

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
  • 46k
4249 votes
35 answers
8.1m views

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
  • 132k

15 30 50 per page
1
2 3 4 5
147183