Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:19:26+00:00 2026-06-07T15:19:26+00:00

This is being implemented with Python and Pygame but it’s a fairly general programming

  • 0

This is being implemented with Python and Pygame but it’s a fairly general programming question (meaning implementation independent).

I have a function which takes as input an x and y integer and should generate a 3×3 grid of neighbouring points (the x and y included).

Note: the 0,0 origin begins at the top left. x increases as you move right, y increases as you move down.

Eg.

def nearest_grid(x, y):
    return [[(x-1,y-1),(x,y-1),(x+1,y-1)],[(x-1,y)(x,y),(x+1,y)],[(x-1,y+1),(x,y+1),(x+1,y+1)]]

So, given a grid and a point (marked p) it returns the following as a list of 3 lists:

x  x  x
x  p  x
x  x  x

Is this the most effective / legible way to do this in Python?

EDIT: Suppose I wanted to pass a radius value (where the above radius value would be 1). So, if I passed a radius value of 2 then the above method would quickly become tiresome. Is there a more general way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:19:28+00:00Added an answer on June 7, 2026 at 3:19 pm
    def nearby_grid_points(x, y, r=1):
        res = []
        for dy in xrange(-r, r+1):
            res.append([(x+dx, y+dy) for dx in xrange(-r, r+1)])
        return res
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am risking this question being closed before i get an answer, but i
How do I use ValueAwareEditor.onPropertyChange? Is this feature implemented already? It is not being
Sorry in advance for this being such a beginner question. Here are the steps
I know this issue being mentioned before, but resolutions there didn't apply. I'm having
Context I apologize for this question being soft / philosophical; it's a set of
I have been scouring the web for something like this and being a lazy
How is this implemented at a python level? I've got an object that pretends
I don’t have much experience with python but I’d like a way to store
I'm brand new to python, but I have a lot of experience with C++,
I guess it's socket programming. But I have never done socket programming expect for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.