Skip to main content
deleted 1644 characters in body
Source Link
Sp3000
  • 62.3k
  • 13
  • 117
  • 292

Builtin functions which are rarely neededAvoid list.insert

Builtins are nice and all, but sometimes they are so wordy that there's shorter alternatives that work better most of the time. Here's a list of some of the common ones.

divmod

Usually it's better to just modulo and divide manually than use divmod, e.g.

a,b=divmod(x,y) a,b=x//y,x%y 

reversed

As mentioned in this tip by @Strigoides, sequences can be reversed with [::-1].

int.bit_length

Checking the lengthInstead of binlist.insert, appending to a slice is usually shorter if parentheses are not required, i.e.:

  
len(bin(n))-2 nL.bit_lengthinsert(i,x) L[:i]+=x, 

The -2 is to accomodate for the 0b prefix..

str.capitalize for single words

Use str.title instead for single words. The difference between the two functions is that capitalize only capitalises the first word, while title capitalises all wordsFor example:

>>> "the quickL brown= fox".capitalize() 'The[1, quick2, brown3, fox'4] >>> "the quick brownL[:-2]+=5, >>> fox".title()L 'The[1, Quick2, Brown5, Fox' 

str.index

str.find is almost always better, and even returns -1 if the substring is not present rather than throwing an exception.

str.startswith

See this tip by @xnor.

str.splitlines

str.split is shorter:

s.splitlines() s.split('\n') 

However, str.splitlines may be useful if you need to preserve trailing newlines, which can be done by passing 1 as the keepends argument.

list.insert

Appending to a slice is shorter:

L.insert(i3,x) 4] >>> L[:i]+=x0]+=6, 

repeat argument for itertools.product

Assuming you've done from itertools import*, you can instead use itertools.tee:

product(>>> L,repeat=n) product(*tee(L[6,n)) 1, 2, 5, 3, 4] 

For n = 2 you don't even need to include n, since 2 is the default argument to tee.

Builtin functions which are rarely needed

Builtins are nice and all, but sometimes they are so wordy that there's shorter alternatives that work better most of the time. Here's a list of some of the common ones.

divmod

Usually it's better to just modulo and divide manually than use divmod, e.g.

a,b=divmod(x,y) a,b=x//y,x%y 

reversed

As mentioned in this tip by @Strigoides, sequences can be reversed with [::-1].

int.bit_length

Checking the length of bin is usually shorter if parentheses are not required, i.e.

 
len(bin(n))-2 n.bit_length() 

The -2 is to accomodate for the 0b prefix..

str.capitalize for single words

Use str.title instead for single words. The difference between the two functions is that capitalize only capitalises the first word, while title capitalises all words:

>>> "the quick brown fox".capitalize() 'The quick brown fox' >>> "the quick brown fox".title() 'The Quick Brown Fox' 

str.index

str.find is almost always better, and even returns -1 if the substring is not present rather than throwing an exception.

str.startswith

See this tip by @xnor.

str.splitlines

str.split is shorter:

s.splitlines() s.split('\n') 

However, str.splitlines may be useful if you need to preserve trailing newlines, which can be done by passing 1 as the keepends argument.

list.insert

Appending to a slice is shorter:

L.insert(i,x) L[:i]+=x, 

repeat argument for itertools.product

Assuming you've done from itertools import*, you can instead use itertools.tee:

product(L,repeat=n) product(*tee(L,n)) 

For n = 2 you don't even need to include n, since 2 is the default argument to tee.

Avoid list.insert

Instead of list.insert, appending to a slice is shorter:

 
L.insert(i,x) L[:i]+=x, 

For example:

>>> L = [1, 2, 3, 4] >>> L[:-2]+=5, >>> L [1, 2, 5, 3, 4] >>> L[:0]+=6, >>> L [6, 1, 2, 5, 3, 4] 
Source Link
Sp3000
  • 62.3k
  • 13
  • 117
  • 292

Builtin functions which are rarely needed

Builtins are nice and all, but sometimes they are so wordy that there's shorter alternatives that work better most of the time. Here's a list of some of the common ones.

divmod

Usually it's better to just modulo and divide manually than use divmod, e.g.

a,b=divmod(x,y) a,b=x//y,x%y 

reversed

As mentioned in this tip by @Strigoides, sequences can be reversed with [::-1].

int.bit_length

Checking the length of bin is usually shorter if parentheses are not required, i.e.

len(bin(n))-2 n.bit_length() 

The -2 is to accomodate for the 0b prefix..

str.capitalize for single words

Use str.title instead for single words. The difference between the two functions is that capitalize only capitalises the first word, while title capitalises all words:

>>> "the quick brown fox".capitalize() 'The quick brown fox' >>> "the quick brown fox".title() 'The Quick Brown Fox' 

str.index

str.find is almost always better, and even returns -1 if the substring is not present rather than throwing an exception.

str.startswith

See this tip by @xnor.

str.splitlines

str.split is shorter:

s.splitlines() s.split('\n') 

However, str.splitlines may be useful if you need to preserve trailing newlines, which can be done by passing 1 as the keepends argument.

list.insert

Appending to a slice is shorter:

L.insert(i,x) L[:i]+=x, 

repeat argument for itertools.product

Assuming you've done from itertools import*, you can instead use itertools.tee:

product(L,repeat=n) product(*tee(L,n)) 

For n = 2 you don't even need to include n, since 2 is the default argument to tee.