Remove odd index values in Python list

There are a few ways you can obtain the odd indices of a list. The methods shown here will be how to obtain the indices and not the actual values stored in the list at the indices. Once you have the indices, you can get the actual values easily.

One way is using the range() function, such that it starts at the first odd index, 1, and has a step value of 2, so that it returns the odd indices 1, 3, 5, ....

range(1, len(list), 2)

Another way to obtain the odd indices of a list is using list comprehension, in which you can use a condition that will only include the odd indices.

[i for i in range(len(lst)) if i % 2 == 1]

One other way is to use a for loop over the indices of a list, and using an if statement that checks whether that index is odd, similar to the condition used in the list comprehension above. This method will not be shown here, but feel free to try implementing it on your own!

Remove odd index values in Python list
code5132969222:

so the difference is that append adds any type of object to a list, but in order to use + or += you must add an object of the same type.?

Not quite: += or lst.extend() work to “append” the entire contents of a list to a given list. The material to be added must be within a list.

Remove odd index values in Python list
code5132969222:

why should i choose append over the += or why should i choose += over append?

It just depends on what you are trying to do.

If I want to add an int to a list of ints, why would I want to create an extra step of first putting my int inside a list just so that I could use +=, when I have append() available?

On the other hand, if I have a list of int whose elements I’d like to tack on to an existing list, why go through the first list and do for-in-append() for each element when I have extend (or += ) available?


Source Code

str="Computer Education"
res = ""
print("Original String :",str) 
for i in range(len(str)):
	if i % 2 == 0:
	  res = res + str[i]
print("Remove Odd Index Char :",res)

Output

Original String : Computer Education
Remove Odd Index Char : Cmue dcto


To download raw file Click Here

In this article, we will discuss different ways to fetch all items at odd index positions of a List in Python.

Table Of Contents

Method 1: Using List Slicing

We can slice the list using subscript operator to select elements at odd index positions only. To slice a list, we need three values,

list[start : end: N]

Here, start and end are index positions. Whereas, N is the step size. It returns, every Nth element from start till end-1. So, to select element at every odd index position, start from index 1, till the end of string, and keep the step size as 2. Let’s see an exaple,

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Get list elements at odd index positions
elements = listOfNumbers[ 1 : : 2]

print(elements)

Output :

Advertisements

[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Method 2: Using enumerate()

The enumerate() function accepts a sequence as arguments, and returns an iterable enumerate object. Which, on iteration, yields pairs containing index position, and value. So, we can pass our list as an argument to the enumerate() function, and iterate over pairs yielded by it. Each pair will have an index position & value at that index position in list. Select only those values for which index is odd. Let’s see an example,

Read More:

  • Check if String Contains Specific Characters in Python
  • Extract even numbers from a list in Python
  • Avoid ValueError exception with List.index() in Python
  • Get Sublist from List based on condition in Python

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Empty List
elements = []

# Get list elements at odd index positions
for index, value in enumerate(listOfNumbers):
    if index % 2 == 1:
        elements.append(value)

print(elements)

Output :

[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Method 3: using List Comprehension

This solution is similar to the previous one, but with just one difference. Although, we will use the enumerate() function and List comprehension to select elements at odd index position in List. But to verify, if an index position is odd or not, we will use the AND Operator. When we apply AND operator to an index position and number 1, if it returns 1, then it means index position is ODD. Let’s see an example,

How do I get only even index of a list in Python?

How to index an list to show even and odd elements in python?.
+ 4. You can use a for loop to iterate on a list and you get in each iteration one element. ... .
+ 3. lst = [your_list] odds = lst[1::2] evens = lst[0::2] ... .
+ 3. ... .

How do you remove the range of an index from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

How do you separate odd and even numbers in a list in Python?

Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the main list. Step 4 : every element is divided by 2, if remainder is 0 then it's even number and add to the even list, otherwise its odd number and add to the odd list.

How do you remove all elements of a certain value from a list in Python?

The remove() method is one of the ways you can remove elements from a list in Python. The remove() method removes an item from a list by its value and not by its index number.