Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A Tips for golfing in Python

Reorder expressions in order to save whitespace Consider the following code: if c=='U':c='X' The space between if and c obviously cannot be removed, as that would merge them to the identifier ...

posted 3y ago by celtschk‭  ·  edited 3y ago by celtschk‭

Answer
#3: Post edited by user avatar celtschk‭ · 2021-09-12T18:19:31Z (about 3 years ago)
Added example noted by @user
  • # Reorder expressions in order to save whitespace
  • Consider the following code:
  • ```
  • if c=='U':c='X'
  • ```
  • The space between `if` and `c` obviously cannot be removed, as that would merge them to the identifier `ifc`. However by reversing the order of the arguments to `==`, the space becomes removable:
  • ```
  • if'U'==c:c='X'
  • ```
  • # Reorder expressions in order to save whitespace
  • Consider the following code:
  • ```
  • if c=='U':c='X'
  • ```
  • The space between `if` and `c` obviously cannot be removed, as that would merge them to the identifier `ifc`. However by reversing the order of the arguments to `==`, the space becomes removable:
  • ```
  • if'U'==c:c='X'
  • ```
  • As [user](https://codegolf.codidact.com/users/53837) noted [in the comments](https://codegolf.codidact.com/comments/thread/4477#comment-13560), another place where whitespace can be saved is between a number and a following keyword. For example, in
  • ```
  • if 2+x in{a,b,c}:x=0
  • ```
  • the space between `x` and `in` cannot be simply removed. However reordering the sum allows to remove it anyway:
  • ```
  • if x+2in{a,b,c}:x=0
  • ```
#2: Post edited by user avatar celtschk‭ · 2021-09-12T06:31:00Z (about 3 years ago)
  • # Reorder expressions in order to save whitespace
  • Consider the following code:
  • ```
  • if c=='U':c='X'
  • ```
  • The space between `if` and `c` obviously cannot be removed, as that would merge them to the identifier `ifc`. However by reversing the order of the arguments to `==`, the space becomes removable:
  • ```
  • if'U'==C:c='X'
  • ```
  • # Reorder expressions in order to save whitespace
  • Consider the following code:
  • ```
  • if c=='U':c='X'
  • ```
  • The space between `if` and `c` obviously cannot be removed, as that would merge them to the identifier `ifc`. However by reversing the order of the arguments to `==`, the space becomes removable:
  • ```
  • if'U'==c:c='X'
  • ```
#1: Initial revision by user avatar celtschk‭ · 2021-09-12T06:30:26Z (about 3 years ago)
# Reorder expressions in order to save whitespace

Consider the following code:
```
if c=='U':c='X'
```
The space between `if` and `c` obviously cannot be removed, as that would merge them to the identifier `ifc`. However by reversing the order of the arguments to `==`, the space becomes removable:
```
if'U'==C:c='X'
```