Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

negative list slicing

# Creating a List
List = ['S','O','F','T','H','U',
        'N','T','.','N','E','T']
print("Initial List: ")
print(List)

# Print elements from beginning
# to a pre-defined point using Slice
Sliced_List = List[:-6]
print("
Elements sliced till 6th element from last: ")
print(Sliced_List)
 
# Print elements of a range
# using negative index List slicing
Sliced_List = List[-6:-1]
print("
Elements sliced from index -6 to -1")
print(Sliced_List)
 
# Printing elements in reverse
# using Slice operation
Sliced_List = List[::-1]
print("
Printing List in reverse: ")
print(Sliced_List)
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #negative #list #slicing
ADD COMMENT
Topic
Name
7+5 =