Home > Net >  How can i remove odd index element from the list?
How can i remove odd index element from the list?

Time:09-22

It show list index out of range. i need to remove all odd index element but it's not working and showing list index out of range

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
        for i in range(len(list1)):
            x=i
            if(i%2!=0):
                #print(list1[x])
                list1.remove(list1[x])
            else:
                continue
        print(list1)

CodePudding user response:

Just do this, it should work

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
list1=list1[::2]
print(list1)`

output:- [12, 45, 75, 4, 66, 1]

CodePudding user response:

Issue:

Your code isn't working for the given snippet because every time you remove an element from list1, an index from the same gets eliminated.

Solution:

To overcome this issue using the iterative method, you need to append the even elements to another list, or as Pratyush Arora stated, any could work.

If you're using the iterative method, this could be helpful:

Code:

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]

# Using list comprehension
evenIndexList = [i for indexValue, i in enumerate(list1) if indexValue % 2 == 0]
print(evenIndexList)

# Standard approach
newList = []
for i in range(len(list1)):
    if i % 2 == 0:
         newList.append(list1[i])
print(newList)

CodePudding user response:

Solution

del list1[1::2]

It's a direct translation from "remove all odd index element" to Python code.

Benchmark along with the upvoted solutions for a list with a million elements:

11.8 ms  11.9 ms  12.0 ms  list1 = list1[::2]
90.1 ms  90.6 ms  91.1 ms  list1 = [i for indexValue, i in enumerate(list1) if indexValue % 2 == 0]
 8.0 ms   8.2 ms   8.2 ms  del list1[1::2]

Benchmark code (Try it online!):

from timeit import repeat

setup = 'list1 = list(range(10**6))'

solutions = [
    'list1 = list1[::2]',
    'list1 = [i for indexValue, i in enumerate(list1) if indexValue % 2 == 0]',
    'del list1[1::2]',
]

for _ in range(3):
    for solution in solutions:
        times = sorted(repeat(solution, setup, number=1))[:3]
        print(*('%4.1f ms ' % (t * 1e3) for t in times), solution)
    print()

CodePudding user response:

Do the following :

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
list1 = [list1[i] for i in range(0,len(list1),2)]
print(list1)

output :

[12, 45, 75, 4, 66, 1]

CodePudding user response:

if you would like to use your way, you can use it:

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
list2 = []
for i in range(len(list1)):
            if(i%2!=1):
                list2.append(list1[i])
print(list2)

list2 contains all even numbers

CodePudding user response:

You can use del

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
del list1[1::2]  # any_list[start: end: steps] Here, end = None
print(list1)

Indexing [1::2]

       ---------------------------------------------------------- 
Items | 12 | 33 | 45 | 66 | 75 | 33 | 4 | 56 | 66 | 67 | 1  | 2  | 
Index |  0 | 1  | 2  | 3  | 4  | 5  | 6 | 7  | 8  | 9  | 10 | 11 |
1::2  |    | ✓  |    | ✓  |    | ✓ |   | ✓  |    | ✓  |    | ✓ |
       ---------------------------------------------------------- 

 
      

So, All tick marked will be deleted from your list.

For learning python refer.

CodePudding user response:

from copy import deepcopy

list1 = [12,33,45,66,75,33,4,56,66,67,1,2]
list2 = deepcopy(list1)
for i in range(len(list2)):
    if(i%2!=0):
        #print(list1[x])
        list1.remove(list2[i])
    else:
        continue
print(list1)

The above issue occurs when we try to remove an element from a list and the same time we are iterating through the same list. To overcome this issue we can create a copy of the list using deepcopy and then iterate through list2. For the removal of the elements use the list1.

Output

[12, 45, 75, 4, 66, 1]
  • Related