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

Time:12-22

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]

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

Ans:

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

Output:

['a', True, 'b', 6.02214076e 23]
  • Related