Skip to main content
Bounty Awarded with 500 reputation awarded by hyperneutrino
added 68 characters in body
Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better!:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

You can also use str instead of set for the same byte count.

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better!:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better!:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

You can also use str instead of set for the same byte count.

added 1 character in body
Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better!:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better!:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.

Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

The shortest infinite for comprehension

You may know the trick to easily create an infinite generator using the two-argument form of iter:

(... for _ in iter(lambda:0,1)) 

where 0 and 1 can be any two non-equal values.

As @ovs once pointed out to me, you can replace lambda:0 with int, because int returns 0 when called with no arguments.

(... for _ in iter(int,1)) 

However, we can do one byte better:

(... for()in iter(set,1)) 

This uses the fact that () is a valid L-value (assignment target) in Python as long as the RHS is an empty sequence, and set returns an empty set when called with no arguments.