4

I have a list of the form L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4] and I want to remove the third index where 1 occurs and replace it with 0. My code is this, but this removes the previous indexes of 1(1st and 2nd also) which I want in my List. My code is this

counter=0
index = 2
L = list([1, 2, 3, 4, 1, 2, 1, 3, 0, 4])
print("Value before is\n", L)
for i in range(len(L)):
    y=L.index(1)
    print(y)
    if(counter==index):
        L[y]=0
        break
    else:
        counter=counter+1
        L[y]
        print("Value of list in else\n",L)
        print("Value of counter\n",counter)

print("After the value is\n",L)

So output comes as

 [2, 3, 4, 2, 0, 3, 0, 4]

but I want it as

L = [1, 2, 3, 4, 1, 2, 0, 3, 0, 4]

and remember that I will not be given directly the index which I want to change So I could Do L[7]=0 Thanks in advance

1
  • While your code is still wrong, the output you announced does not match the actual output, can you fix that? Commented Jun 14, 2018 at 13:33

3 Answers 3

3

There are a few issues with your algorithm, but it boils down to this: by doing y = L.index(1) you find the first index where a 1 appears. So by doing L[y] = 0, all you can do is update the first occurence of a 1.

Finding the nth index

There is no builin to find the nth appearance and so you will have to write it.

To be consistent with list.index, I made the following index function raise a ValueError when the item is not found.

Code

def index(lst, obj, n=1):
    count = 0
    for index, item in enumerate(lst):
        if item == obj:
            count += 1
        if count == n:
            return index
    raise ValueError('{} is not in list at least {} times'.format(obj, n))

L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]

index = index(L, 1, n=3)

L[index] = 0

print(L)

Output

[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]

Using list-comprehension

Alternatively, if all you want to do is replace the nth occurence, but do not care about its actual index, you can generate a new list with a list-comprehension and an itertools.count object.

Code

from itertools import count

def replace(lst, obj, repl, n=1):
    counter = count(1)
    return [repl if x == obj and next(counter) == n else x for x in lst]


L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]

new_list = replace(L, 1, 0, n=3)
print(new_list)

Output

[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]
Sign up to request clarification or add additional context in comments.

2 Comments

@ShivamSharma It looks like you tried to ask a question via editing my answer. If you want clarifications, please ask so in comments.
@ShivamSharma Although, in the question you asked in the edit, you replace count(1) by count(obj). This makes the counter start at obj. You always want it to start at 1.
3

Using enumerate

Demo:

index = 2
value = 1
c = 0
L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]

for i, v in enumerate(L):
    if v == value:
        if c == index:
            L[i] = 0
            break
        else:
            c+=1
print( L )

Output:

[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]

Comments

1

You could set up a counter variable called timesOccured for the amount of times the integer 1 occurs in the list.

Once timesOccured == 3 and L[i] == 1 then you can swap the value of L[i] for 0 like so:

if timesOccured == 3 and L[i] == 1:
    L[i] = 0

I would also suggest getting rid of the counter variable unless it is necessary for another portion of your program. The range function in for i in range(L) returns a list which contains all the indices of L. In this case, range(L) would return [0,1,2,3,4,5,6,7,8,9]. The for loop would then iterate through this list, assigning the current value stored in the index of the current iteration in i.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.