仅获取 Python 列表的偶数或奇数索引元素
Get only the even index elements of a Python List
使用列表切片将偶数/奇数索引元素拆分为两个列表,例如
even = my_list[::2]
和odd = my_list[1::2]
。
第一个列表切片将仅包含具有偶数索引的元素,第二个列表切片将包含具有奇数索引的元素。
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # ✅ get all elements with an even index in the list even = my_list[::2] print(even) # 👉️ [0, 2, 4, 6, 8, 10] # --------------------------------------------- # ✅ get all elements with an odd index in the list odd = my_list[1::2] print(odd) # 👉️ [1, 3, 5, 7, 9] # --------------------------------------------- # ✅ get every second element of the list (starting with the second element) n = 2 every_second = my_list[n - 1::n] print(every_second) # 👉️ [1, 3, 5, 7, 9] # --------------------------------------------- # ✅ get every second element of the list (starting at index 2) every_second = my_list[2::2] print(every_second) # 👉️ [2, 4, 6, 8, 10] # ---------------------------------------- # ✅ using a for loop even = [] odd = [] for index, item in enumerate(my_list): if index % 2 == 0: even.append(item) else: odd.append(item) print(even) # 👉️ [0, 2, 4, 6, 8, 10] print(odd) # 👉️ [1, 3, 5, 7, 9]
我们使用列表切片来仅获取列表的偶数索引元素。
列表切片的语法是my_list[start:stop:step]
.
start
索引是包含的,索引stop
是排他的(最多,但不包括)。
0
, and the last item has an index of -1
or len(my_list) - 1
.The slice my_list[::2]
gets the even index elements of the original list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = my_list[::2] print(even) # 👉️ [0, 2, 4, 6, 8, 10]
The start
and end
indexes are omitted, so the slice starts at index 0
and
goes to the end of the list.
step
value is set to 2
, so the slice selects every second element, starting at index 0
.You can use the same approach to get only the elements with an odd index of a
list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # ✅ get all elements with odd index in the list odd = my_list[1::2] print(odd) # 👉️ [1, 3, 5, 7, 9]
Instead of starting at index 0
, the slice starts at index 1
and selects
every second element.
The stop
index is omitted, so the slice selects the odd elements of the entire
list.
If you need to get every Nth element of a list, specify a value for the start
index.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # ✅ get every second element of the list (starting at second element) n = 2 every_second = my_list[n - 1::n] print(every_second) # 👉️ [1, 3, 5, 7, 9]
The example selects every second element of the list starting at the second
element.
If you need to get the elements with an even index of a list, starting at a
specific index, set the value for the start
index.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] every_second = my_list[2::2] print(every_second) # 👉️ [2, 4, 6, 8, 10]
The new list consists of the even elements of the original list starting at
index 2
(the third element).
Alternatively, you can use a for
loop.
Get only the even or odd index elements of a Python List using a for
loop #
To split the even and odd index elements into two lists:
- Declare
even
andodd
variables and set them to empty lists. - Iterate over the original list.
- Push the even index elements into the
even
list and theodd
index
elements into theodd
list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = [] odd = [] for index, item in enumerate(my_list): if index % 2 == 0: even.append(item) else: odd.append(item) print(even) # 👉️ [0, 2, 4, 6, 8, 10] print(odd) # 👉️ [1, 3, 5, 7, 9]
The enumerate
function takes an iterable and returns an enumerate object containing tuples
where the first element is the index, and the second is the item.
On each iteration, we use the modulo operator to check if the current index is
even or odd.
The
modulo (%)
operator returns the remainder from the division of the first value by the
second.
print(10 % 2) # 👉️ 0 print(10 % 4) # 👉️ 2
The last step is to append the even indexes to the even
list and the odd
indexes to the odd
list.