0

I have a list li = [1,2,3,4], I need to remove odd index from list each time, after removing odd index from a list I will get 2,4 then after again I need to remove odd element and I will get 4, which should be my output.

I tried solving this in Python:

li = [1,2,3,4,5]
arr = []
for i in range (len(li)):
    if(i%2 == 0):
        continue
    arr.append(li[i])
for i in range (len(arr)):
    if(i%2 == 0):
        continue
    print (arr[i])

But for same array i.e., li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], i will get [4,8,12] and again removing odd indices from arr = [4,8,12]. I will get output 8 by removing odd index element. How can I do with more optimal solution?

  • I don't understand... if you remove odd indices 2 times, then from `li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]` you should get `[4, 8, 12]`. – Andrej Kesely Apr 01 '21 at 09:48
  • I think the question will be to keep removing odd indices while arr has more than 1 element. – starboy_jb Apr 01 '21 at 12:28
  • Yes, exactly keep removing odd indices while arr has more than 1 element and if I remove odd indices two times, then from li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] I should get [4,8,12] then again I have to remove odd indices from [4,8,12], so that my output will be 8 in this case, so how can I do without using any inbuilt function or without using list slicing method in recursion. – Pandit kumar Apr 01 '21 at 13:15
  • https://stackoverflow.com/questions/28883769/remove-odd-indexed-elements-from-list-in-python use this plus a recursive function that exits when 1 element is left – nathan hayfield Apr 01 '21 at 17:07
  • if you don't want to use list slicing and recursion then you need a while loop to check if your array has one element and then one method to loop and remove elements that gets called in the while loop – nathan hayfield Apr 01 '21 at 17:41
  • Why would you do this at all? The element that will remain is the one with index that is the highest power of 2. Repeatedly modifying the list seems pointless. – William Pursell Apr 01 '21 at 19:16
  • @WilliamPursell lol it does seem pointless – nathan hayfield Apr 02 '21 at 16:52

1 Answers1

0

based on this answer: Remove odd-indexed elements from list in Python

def removeOddIndexesTilOneElement(x):
    if len(x) == 1:
        return x

    del x[1::2]
    return removeOddIndexesTilOneElement(x)

li = [1,2,3,4,5]
print(removeOddIndexesTilOneElement(li))

Note that in your example with 1,2,3,4 my function will actually delete 2 and 4 because their indexes are 1 and 3. The index starts with 0. You might have to tweak this to remove even indexes if you want to get a result of 4. You could change one line to: del x[0::2] to get rid of even indexes.

No recursion, no slice trick method:

def removeOddIndex(x):
    ret = []
    for i in range (len(x)):
        if i%2 == 0:
            ret.append(x[i])
    return ret        
        

li = [1,2,3,4]

while len(li) > 1:
    li = removeOddIndex(li)

print(li)
nathan hayfield
  • 2,627
  • 1
  • 17
  • 28