Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 3860

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

1 vote

Pythonic split list into n random chunks of roughly equal size

Make it a generator. You could then simplify the logic. def chunk(xs, n): ys = list(xs) random.shuffle(ys) chunk_length = len(ys) // n needs_extra = len(ys) % n start = 0 for …
Jeff Mercado's user avatar
  • 5,432
2 votes

Dictionary-based dispatch in Python with multiple parameters

You could change it to just store the functions and the arguments. Then use the argument unpacking to fill in the correct arguments to the function call. action = 'op2' functions = { 'op1': (pri …
Jeff Mercado's user avatar
  • 5,432
3 votes

How can I make handling multiple {}.get('value', {}.get('foo')) pretty?

Your code attempts to fetch the values of all keys when it probably only really needs one. You do not want to be doing this. I think it would be better to create a list representing the priorities o …
Jeff Mercado's user avatar
  • 5,432
1 vote
Accepted

Method to count interior nodes of a binary tree

So after our little discussion, I hope you see that you're missing some key cases here, when the root given is None. Some things that should be pointed out, when comparing to None, you should be chec …
Jeff Mercado's user avatar
  • 5,432
3 votes
Accepted

Singleton Decorator

It might be beneficial to add positional and keyword arguments to the __call__() method. That way you'll get your SingletonError for any call you attempt to make, rather than just the no-argument cal …
Jeff Mercado's user avatar
  • 5,432
3 votes

Retrieving lists of consecutive capitalised words from a list

If you asked me, a pythonic solution would be the most readable solution. However in general, readable solutions are not always going to be the fastest. You can't have both, these are conflicting go …
Jeff Mercado's user avatar
  • 5,432
3 votes

Finding perimeters of right triangles with integer-length legs, up to a limit

You can rewrite it with two separate comprehensions. One to generate the values and one to filter them out. You can use itertools.product to get the cartesian product. a = [x for x in (i + j + (i** …
Jeff Mercado's user avatar
  • 5,432
7 votes

Ultra-Beginner Python FizzBuzz ... Am I missing something?

Your code looks fine. It certainly works. The only thing I'd say about it is that you are repeating the incrementation in all if blocks. You could just move that out of and after them and you'll ac …
Jeff Mercado's user avatar
  • 5,432
18 votes

Grouping consecutive numbers into ranges in Python 3.2

You could use this one-liner to generate groups of consecutive integers in a list: from itertools import groupby, count groupby(numberlist, lambda n, c=count(): n-next(c)) Then to finish it off, g …
Jeff Mercado's user avatar
  • 5,432
1 vote

Quadrant Queries challenge

Consider changing your algorithm to perform the task. Every operation that you need to do takes some calculation to perform the operation. Not necessarily cheap as far as the test cases thrown at yo …
Jeff Mercado's user avatar
  • 5,432