0
Hello peps sorry but I really don't understand the concept and how to do list slices
Is it possible that someone help me out with list slices the single and double thingy I think how to do it pleas anyone help me. Much appreciated
2 Answers
+ 1
list = [1,2,3,4,5,6,7,8]
print (list [2:4])
print (list [:3])
print (list[2:])
OUT PUT =
[3,4,5]
[1,2,3,4]
[3,4,5,6,7,8]
The slice is the sections of the list which is printed. The slice is the value in [] (3:5 for example). This represents which parts (sorry for my bad programming language :) ) of the list are printed -the third to the fifth. However, remember that python starts counting at 0!
If you were to write [ : x], everything before x would be printed.
If you were to write [x:], everything after x would be printed.
Hope this helps!
0
EDIT:
The second output would be [1,2,3] as only the numbers BEFORE that specified would be printed.
Also, the last explanation is slightly wrong - all the numbers after AND INCLUDING that number are printed.
Sorry for my earlier mistakes :)