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

  • SEARCH
  • Home
  • 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 7402743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:49:04+00:00 2026-05-29T04:49:04+00:00

while i’m interating a list, how can i get the id of the current

  • 0

while i’m interating a list, how can i get the id of the current item to reference it to list methods?

xl = [1,2,3] # initial list
yl = [3,2]   # list used to remove items from initial list

for x in xl[:]:
    for y in yl:
        if x == y:
            xl.pop(x) # problem
            break
        print x, y
print xl

in the simple example, i want to loop through the 2 lists, and when i find a similar item, remove it from list 1.

What should i use instead of X in the line commented with “#problem”?

PS: Note it’s a copy i’m iterating from.

  • 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-29T04:49:05+00:00Added an answer on May 29, 2026 at 4:49 am

    The general way to do this is with enumerate.

    for idx, item in enumerate(iterable):
      pass
    

    But for your use case, this is not very pythonic way to do what you seem to be trying. Iterating over a list and modifying it at the same time should be avoided. Just use a list comprehension:

    xl = [item for item in xl if item not in yl]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I can get a list of pages within a specific category using 'categorymembers',
While most of the time I'd just use file_get_contents and CURL, I can't get
While I can successfully ping a global address from my teredo-enabled end-host, I'm unable
While going through university and from following the development of SO, I've heard a
While working in a Java app, I recently needed to assemble a comma-delimited list
While issuing a GET request using Apache HTTP Client v4, how do I obtain
While in diff mode ( Show Diff ) how can I go back immediately
While learning on anonymous methods, i've found the following example on the internet: namespace
While fetching Boolean data from database with hibernate SQL query gives the following error
while i'm using push notification in iphone with local server(used php), not able to

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.