Home > Blockchain >  Python difference between [:-1] and [::-1]
Python difference between [:-1] and [::-1]

Time:10-01

I was asking myself what the difference is between:

[:-1] and [::-1]

CodePudding user response:

[:-1] will give you the output from first element to 2nd last element

[::-1] will reverse your given input

e.g.;

s = "sachin"
s[:-1]             # 'sachi'
s[::-1]            # 'nihcas'

CodePudding user response:

Understand in a practical way using list:

list = ["manoj", "saloni", "soumya", "ankita", "kiran"]
print(list[:-1])
print(list[::-1])

Output:

['manoj', 'saloni', 'soumya', 'ankita']

['kiran', 'ankita', 'soumya', 'saloni', 'manoj']
  • Related