slicing

Run Settings
LanguagePython
Language Version
Run Command
l = [1,2,3,4,5] x = l[1:2:1] print(x) x = slice(1,3,1) #1:3:1 print(type(x)) l = list(range(10)) # l = tuple(range(10)) print(l) print(l[x]) top_4 = slice(1,3,1) print(l[top_4]) last_4 = slice(-1,-5,-1) # print(last_4) print(l[last_4]) print(l[1:3:1]) s = {1,2,3,4,5,6,7,8,9} # s[1:3:1] print(last_4.start) print(last_4.stop) print(last_4.step) print(last_4) # print(l[10]) top_4 = slice(1,100,1) print(l[top_4]) l = [1,2,3,4,5,6,7,8,9] print(l[1:15:3]) print(l[-1:-4:-2]) l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] #16 # 0,1,2,3,4,5,6,7,8, 9,10,11,12,13,14,15 # -16,-15,-14,... -3,-2,-1 print(l[1:22:3]) print(l[-3:-12:-3]) #[16-3][16-12] print(l[13:4:-3]) print(l[3:-5:4]) print(l[14:-14:-4]) print(l[-10:14:3]) print(l[-14:-3:3]) # 2:13 print(l[-13:2:-4]) # 3:2 print(l[::-1]) print(l[-4:-99:-1]) #------------ +step # if start > len(list) => start = len(list) print(l[16:99:1]) # if end > len(list) => end = len(list) => until last element of the list print(l[15:99:1]) print(l[99:99:1]) # if start < 0, start = max(0, len(list) + start) print(l[-99:12:-1]) print(l[-99:5:1]) print(l[-99:-99:1]) # if end < 0, end = max(0, len(list) + end) # hint : ending value must be smaller than len(list); otherwise empty list print(l[-99:-99:1]) print(f' end = -99 : {l[:-99]}') print("end = -99 : {0}".format(l[:-99])) print("end = -99 :", l[:-99], "something else") # if start is missing or None start = 0 # if end is missing or None end = len(list) => until last element of the list print(l[None:12:1]) print(l[1::]) #------- -Step # if start > len(list); start = len(list) - 1 => starting from the last element print(l[99:13:-1]) print(l[99::-1]) # print(l[99:99:1]) # print(l[99:16:1]) # print(l[10:99:1]) # if end > len(list); end = len(list) - 1 + stop at out of bound, [] print(l[15:99:-1]) # if start < 0; start = max(-1, len(list) + start) => start from last if |-vs| > len(list) => empty [] print(l[-3:6:-1]) print(l[-99:6:-1]) print(l[-16:6:-1]) # if end < 0; end = max(-1, len(list) + end) => ending at the beginning of the list print(l[7:-99:-1]) x = l[-4:-1] print(x[::-1]) # if start is missing or None; start = len(list) - 1 => from the end of the list print(l[:5:-1]) # if end is missing or None; end = -1 (index, not positive number) => beginning of the list print(l[2::-1]) print(l[2::-2]) print(l[::-1]) print(l[-3:-12:-3]) #[#13,..#5] [14,11,8] print(l) l[2:4] = (4,5,6,7) print(l) l[2:4] = "a" print(l) l[2:3] = [7,8] print(l) l[2:3] = "ab" print(l) del l[5:7] print(l) l[2:3] = ["ab"] print(l) l[2:3] = ("cd",) # l[2:3] = ("cd") print(l) # l[2:5:2] = ["ab","cde","fgh"] l[2:7:2] = ["ab","cde","fgh"] print(l) l[2:3] = [i**2 for i in range(5) if i != 3] print(l) l[2:3] = [i**2 for i in l if type(i) == int] print(l) print(16*16)
Editor Settings
Theme
Key bindings
Full width
Lines