0

i wrote a script in python that removes a particular character from a python list. Here i chose to remove all 'h' in a list. This is the code i wrote:

input1=list(raw_input("Enter input list:"))
print "Initial list:",input1  
for item in input1:

    if item == 'h':
        input1.remove(item)
        print input1
    else:
        continue
print "Final list:",input1

I gave the following input:

hhhhheilhhthhh

I expected the final list to be :

['e','i','l','t']

i got the following output:

['e', 'i', 'l', 'h', 't', 'h', 'h', 'h']

This is the whole output:

Enter input list:hhhhheilhhthhh
Initial list: ['h', 'h', 'h', 'h', 'h', 'e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['h', 'h', 'h', 'h', 'e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['h', 'h', 'h', 'e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['h', 'h', 'e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['h', 'e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['e', 'i', 'l', 'h', 'h', 't', 'h', 'h', 'h']
['e', 'i', 'l', 'h', 't', 'h', 'h', 'h']
Final list: ['e', 'i', 'l', 'h', 't', 'h', 'h', 'h']

I think the problem is with the remove function. Why does python behave like this?

Gadheyan .t.s
  • 377
  • 2
  • 9
  • 23

0 Answers0