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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:08:14+00:00 2026-05-16T00:08:14+00:00

Hey, I was trying to delete an item form a list (without using set

  • 0

Hey, I was trying to delete an item form a list (without using set):

list1 = []
for i in range(2,101):
    for j in range(2,101):
        list1.append(i ** j)
list1.sort()
for k in range(1,len(list1) - 1):
    if (list1[k] == list1[k - 1]):
        list1.remove(list1[k])
print "length = " + str(len(list1))

The set function works fine, but i want to apply this method. Except I get:

 IndexError: list index out of range

on the statement:

 if (list1[k] == list1[k - 1]):

Edited to add
(Thanks to Ned Batchelder) the working code is:

list1 = []
for i in range(2,101):
 for j in range(2,101):
   list1.append(i ** j)
list1.sort()
k = 0
while k < len(list1) - 1: # while loop instead of for loop because "The range function is evaluated once before the loop is entered"
 k += 1
 if (list1[k] == list1[k - 1]):
  list1.remove(list1[k])
  list1.sort()
  k -= 1 # "If you find a duplicate, you don't want to move onto the next iteration, since you'll miss potential runs of more than two duplicates"
print "length = " + str(len(list1))
  • 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-16T00:08:15+00:00Added an answer on May 16, 2026 at 12:08 am

    Your code doesn’t work because in your loop, you are iterating over all the indexes in the original list, but shortening the list as you go. At the end of the iteration, you will be accessing indexes that no longer exist:

    for k in range(1,len(list1) - 1):
        if (list1[k] == list1[k - 1]):
            list1.remove(list1[k])
    

    The range function is evaluated once before the loop is entered, creating a list of all the indexes in the list. Each call to remove shortens the list by one, so if you remove any elements, you’re guaranteed to get your error at the end of the list.

    If you want to use a loop like this, try:

    k = 1
    while k < len(list1):
        if list1[k] == list1[k-1]:
            del list1[k]
        else:
            k += 1
    

    I fixed a few other things:

    1. You don’t need parentheses around the condition in Python if statements.
    2. If you find a duplicate, you don’t want to move onto the next iteration, since you’ll miss potential runs of more than two duplicates.
    3. You want to start from index 1, not zero, since k=0 will access list1[-1].
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 470k
  • Answers 470k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the TotalHours property or other Total[TimeUnit] properties in the… May 16, 2026 at 2:56 am
  • Editorial Team
    Editorial Team added an answer It's always worth setting the prefetch count when using basic.consume… May 16, 2026 at 2:56 am
  • Editorial Team
    Editorial Team added an answer This is a code written by me and a friend… May 16, 2026 at 2:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.