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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:24:51+00:00 2026-06-12T23:24:51+00:00

I have a file I am putting into a list and I am wanting

  • 0

I have a file I am putting into a list and I am wanting to take some things out of it, and append to another list ( I have no problem with that), the problem I run into is removing the things out of the first list. below is what I have tried but it only removes every other from the original list.

list:
bob     g1  3110
bob     g2  244
bob     g3  -433
greg    fun112  10595
greg    fun113  -1203
greg    fun114  -3049.999
greg    fun115  3808
greg    fun116  320
greg    got112  -600
greg    got113  5958
greg    got114  1249


file1 = open('test','rb').read().splitlines()
file1=sorted(file1)
test_group = ['fun','got']
test= []

for product in file1:
    spt = product.split(",")
    for line in spt:
        if line[:3] in test_group:
            x = test.append(product)
            y = file1.remove(product)

the test [ ] list is fine all of the items I want in there go with no problem, but when I review file1 it only takes out every other one of ‘fun’ and ‘got’ lines

why is this only taking out every other one, and how do I fix it?

  • 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-12T23:24:52+00:00Added an answer on June 12, 2026 at 11:24 pm

    Don’t try to modify a list you’re iterating over! That’s not going to work!

    If you make a copy of the list, then it should work:

    for product in file1[:]:
        spt = product.split(",")
        for line in spt:
            if line[:3] in test_group:
                x = test.append(product)
                y = file1.remove(product)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an api which I'm putting things into and out of in a
I have a UserControl which rather than putting tags into its ascx markup file,
I have a function that reads through a file, and gathers the results into
I have a sql database that stores some documents. A user can sign into
I'm thinking about putting a .txt file in res/drawable or in res/layout to have
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
I have a textfile which my Java program is modifying and putting into an
I have an xml file that has a start date and length of a
What I have is a long list of codes that involves reading different files
Greetings, i got into the following problem: Given a file of the following structure:

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.