If I have the list:
lista=[99, True, "Una Lista", [1,3]]
What does the following expression mean?
mi_var = lista[0:4:2]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The syntax
lista[0:4:2]is called extended slice syntax and returns a slice of the list consisting of the elements from index 0 (inclusive) to 4 (exclusive), but only including the even indexes (step = 2).In your example it will give
[99, "Una Lista"]. More generally you can get a slice consisting of every element at an even index by writing lista[::2]. This works regardless of the length of the list because the start and end parameters default to 0 and the length of the list respectively.One interesting feature with slices is that you can also assign to them to modify the original list, or delete a slice to remove the elements from the original list.