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

Comments on Tips for golfing in Python

Parent

Tips for golfing in Python

+2
−0

If you have any tips for golfing in Python, add them as answers to this post.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+2
−0

Combine loops

Suppose you have a for loop in another, maybe a couple of times, and nothing else inside of the outer for loops (except for the first for loop since anything outside won't be involved at all). It's probably a range() problem again.

If you're doing something like this:

x=10;y=15
for i in range(x):
	for j in range(y):
		print(i,"-",j)

Try it online!

You can simply set the loop as one:

x=10;y=15
for i in range(x*y):print(i//y,"-",i%y)

Try it online!

Golfing 16 bytes in total. You can do this with more loops, but this is what's functional for now.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Further golfing (1 comment)
Further golfing
CrSb0001‭ wrote 3 days ago

We can further golf this using the following snippet:

for i in range(150):print(i//15,'-',i%15)

This is 41 bytes (compared to your 49), unless we need x and/or y later.

As an alternative, if we need to use y later on, we can do this for 45 bytes:

y=15
for i in range(10*y):print(i//y,'-',i%y)