List Slices (Python) | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

List Slices (Python)

I am so confused in this question of list slices squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[::2]) print(squares[2:8:3]) The second part of this question is little confusing for me . How?? the answer came ? Can anybody helping to solve this problem .

22nd Nov 2020, 1:18 PM
Aayush Meshram
2 Réponses
+ 2
squires[::2] Start,end index not mentioned here so default it will 0 to length of list.. There 2 is step count to consider for next index so here values from 0 to len(squires) by step count 2 will be extracted so taken at indexes 0, 0+2, 2+2, 4+2, 6+2 are extracted 8+2 is out of range and end index is excluded.. Output : Values at indexes 0,2,4,6,8 are 0,4,16,36,64 squires[2:8:3] slicing starts at index 2, upto 8(not inclusive) with step count 3 so indexes taken count are 2, 2+3=5, 5+3=8 but it is out of range given for slicing so only at indexes 2,5 are extracted those are 4,25. Slicing syntax : list[start : end : step_count] If start value ommited then default is 0. For end, default value is len(list) For step_count is 1 default value..
22nd Nov 2020, 9:48 PM
Jayakrishna 🇮🇳
22nd Nov 2020, 3:29 PM
Ratnapal Shende
Ratnapal Shende - avatar