• Register
0 votes
37 views

Problem:

I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code:
Nums = [11, 22, 11, 33, 11, 44, 11, 55]
a = 11
print ("Original Numbers:")
print (Nums)
i=0 
length = len(Nums)   
while(i<length):
	if(Nums[i]==a):
		Nums.remove (Nums[i])
		length = length   
		continue
	i = i+1
print ("list after removing Numbers:")
print (Nums)

Which gives me following error:

Original Numbers:
[11, 22, 11, 33, 11, 44, 11, 55]
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    if(Nums[i]==a):
IndexError: list index out of range

 

Why I am getting this output but not the actual result. Please help

10 7 2
6,060 points

Please log in or register to answer this question.

2 Answers

0 votes

Answer:

In your program you have some logical problem.You are getting error beacuse of

length = length  

this line. You should have written this line like:

length = length -1  

And your code would be like:

Nums = [11, 22, 11, 33, 11, 44, 11, 55]
a = 11
print ("Original Numbers:")
print (Nums)
i=0 
length = len(Nums)   
while(i<length):
	if(Nums[i]==a):
		Nums.remove (Nums[i])
		length = length -1  
		continue
	i = i+1
print ("list after removing Numbers:")
print (Nums)

If you use this code you should get an output like:

Original Numbers:
[11, 22, 11, 33, 11, 44, 11, 55]
list after removing Numbers:
[22, 33, 44, 55]

Happy Coding.

11 6 4
34,950 points
0 votes

Given a list, and we have to remove all occurrences of a given element from the list in python.

Example:

For example, the user enters the following input;

  list = [10, 20, 10, 30, 10, 40, 10, 50]

    n = 10

and we want to display the following output;

list after removing the 10 from the list;

list = [20, 30, 40, 50]

 Code for example:

# list with integer elements

list = [10, 20, 10, 30, 10, 40, 10, 50]

# number (n) to be removed

n = 10

# print original list 

print ("Original list:")

print (list)

# loop to traverse each element in list

# and, remove elements 

# which are equals to n

i=0 #loop counter

length = len(list) #list length 

while(i<length):

     if(list[i]==n):

           list.remove (list[i])

           # as an element is removed    

           # so decrease the length by 1 

           length = length -1  

           # run loop again to check element                                         

           # at same index, when item removed 

           # next item will shift to the left 

           continue

     i = i+1



# print list after removing given element

print ("list after removing elements:")

print (list)

Logic:

  • Run a while loop from the 0th element to the last element index.
  • Check the element whether it is equal to the number or not, which we want to remove.
  • If any element of the list is equal to the number, which is to be removed. Then remove that element from the list.
  • To remove the number from the list, use the list.remove() method.
  • After removing the element from the list, decrease the length, because one item is deleted, then continue the loop to check the next item at the same index (after removing the element, next element will be shifted to the previous index).
  • If the element not found, then increase the loop counter to check the next element.

 

I hope this will help to solve your problem!

11 5 2
3,890 points

Related questions

0 votes
1 answer 59 views
59 views
Problem : How to remove all occurrences of a value from a list? I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 35 views
35 views
Problem: I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is: def DeletChar(e, b) : counts = e.count(b) e = list(e) while counts : e.remove(b) counts -= 1 print(e) if __name__ == '__main__' : e = "geeksforgeeks" DeletChar(e,'g') The output ... e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's'] how to solve this problem I am a newbee please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 13 views
13 views
Problem: The question is to traverse given string, as soon as we find an occurrence of we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
asked Nov 2, 2020 Mashhoodch 9.1k points
1 vote
1 answer 45 views
45 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn&rsquo;t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
5 views
Problem: have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't ... [3], [4]] I don't care about preserve order. Note: this question is similar but not quite what I need. Searched SO but didn't find exact duplicate.
asked 6 days ago Mashhoodch 9.1k points
0 votes
1 answer 6 views
6 views
Problem: items = [] items.append("apple") items.append("orange") items.append("banana") // FAKE METHOD:: items.amount() // should return 3 How can I do that?
asked Feb 15 sasha 8.4k points
1 vote
1 answer 78 views
78 views
Problem: I have a list that contains the values: list = [2, 4, 8, 16, 32, 2, 64, 128, 2, 256, 512, 2] I wrote a python program for this where I could find out how many times a value appears in a list python: def count_appearence(list, z): check = ... count_appearence(list, z))) NameError: name 'z' is not defined What is the problem with my code. Please help me solve this thing. Thanks in advance.
asked May 13, 2020 samhaz 5k points
0 votes
2 answers 454 views
454 views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 6 views
1 vote
1 answer 27 views
27 views
Problem: Hello Kodlogs, I am a newbie in Python programming. I am learning Python by enrolling in an online course. I am solving entry-level problems from some websites. I was doing well unless I get a question that asked for count occurrences of a particular item in a ... light on this? Please, help me to solve the problem. I am looking forward to having your answers. Thanks a lot in advance.
asked Jul 31, 2020 adamSw 11.3k points