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!
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.
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 HereIn 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
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,