All Questions

1736
votes
29answers
941288 views

Making a flat list out of list of lists in Python

I wonder whether there is a shortcut to make a simple list out of list of lists in Python. I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with reduce, but I get an ...
139
votes
5answers
5157 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 ...
156
votes
14answers
401848 views

How can I read inputs as integers?

Why does this code not input integers? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to i...
7556
votes
37answers
1552992 views

What does the "yield" keyword do?

What is the use of the yield keyword in Python? What does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchi...
333
votes
7answers
192364 views

Short Description of the Scoping Rules?

What exactly are the Python scoping rules? If I have some code: code1 class Foo: code2 def spam..... code3 for code4..: code5 x() Where is x found? Some possible c...
1324
votes
15answers
339594 views

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

In the following method definitions, what does the * and ** do for param2? def foo(param1, *param2): def bar(param1, **param2):
3195
votes
45answers
2268423 views

Calling an external command in Python

How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
3468
votes
20answers
1458732 views

What does if __name__ == "__main__": do?

What does the if __name__ == "__main__": do? # Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() time.sleep(sleepti...
2241
votes
19answers
2379282 views

Using global variables in a function other than the one that created them

If I create a global variable in one function, how can I use that variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?
2581
votes
38answers
1560616 views

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but how...

Previous Next