why can't I get the 2nd item, including the zeroth from my list

Question:

my_list = ["a", 1.3, True, 17, "b", "c", 6.02214076e23, False]

I am trying to write a code to get the 2nd term including the Xero term,and I’ve tried this code:

my_list[::2]
print(my_list)

but the code is still returning as

my list:
['a', 1.3, True, 17, 'b', 'c', 6.02214076e+23, False]

the code I tried

my_list[::2]
print(my_list)

but it’s still giving me a copy of the full list again:
['a', 1.3, True, 17, 'b', 'c', 6.02214076e+23, False]

Asked By: Math Siv

||

Answers:

Slicing does not modify the list. Assign the slice to another variable or reassign the result back to my_list.

res = my_list[::2]
print(res)
Answered By: Unmitigated

The expression does not modify the list. You can fix this by assigning the output value back to a variable:

res = my_list[:2]

print(res)
Answered By: Xiddoc

Ans:

my_list   = ["a", 1.3, True, 17, "b", "c", 6.02214076e23, False]
print(my_list[::2])

Output:

['a', True, 'b', 6.02214076e+23]
Answered By: Nissan Apollo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.