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 6357901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:15:45+00:00 2026-05-24T23:15:45+00:00

to get right down to it, I’m trying to iterate through a list of

  • 0

to get right down to it, I’m trying to iterate through a list of coordinate pairs in python and delete all cases where one of the coordinates is negative. For example:

in the array:

map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]

I want to remove all the pairs in which either coordinate is < 0, leaving:

map = [[2, 3], [7, 1]]

My problem is that python lists cannot have any gaps, so if I loop like this:

i = 0
for pair in map:
        for coord in pair:
            if coord < 0:
                del map[i]
    i += 1

All the indices shift when the element is deleted, messing up the iteration and causing all sorts of problems. I’ve tried storing the indices of the bad elements in another list and then looping through and deleting those elements, but I have the same problem: once one is gone, the whole list shifts and indices are no longer accurate.

Is there something I’m missing?

Thanks.

  • 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-05-24T23:15:46+00:00Added an answer on May 24, 2026 at 11:15 pm

    If the list is not large, then the easiest way is to create a new list:

    In [7]: old_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
    
    In [8]: new_map=[[x,y] for x,y in a_map if not (x<0 or y<0)]
    
    In [9]: new_map
    Out[9]: [[2, 3], [7, 1]]
    

    You can follow this up with old_map = new_map if you want to discard the other pairs.

    If the list is so large creating a new list of comparable size is a problem, then you can delete elements from a list in-place — the trick is to delete them from the tail-end first:

    the_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
    for i in range(len(the_map)-1,-1,-1):
        pair=the_map[i]
        for coord in pair:
            if coord < 0:
                del the_map[i]
    
    print(the_map)
    

    yields

    [[2, 3], [7, 1]]
    

    PS. map is such a useful built-in Python function. It is best not to name a variable map since this overrides the built-in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to get the mouse direction (Left, Right, Up, Down) based on
Given an RFC822 message in Python 2.6, how can I get the right text/plain
been working all day on something and can't seem to get this right... I
I'm working on a page located here: http://www.fusionhost.co.uk/newsite2/ I'm trying to get the right
I'm trying to get one div to float left, and one div to float
I'll get right to the point. This is what a browser request looks like
I have this design which I cannot seem to get right, I would like
My last post was met by smarmy, unhelpful answers (comments), so i'll get right
//I can get the right margins by defining a rectangle and giving it the
How i can get full right name of generic type? For example: This code

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.