Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

negative slicing in python list

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Comment

negative slicing in python

>>> l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']

# I want a string up to 'def' from 'vwx', all in between
# from 'vwx' so -2;to 'def' just before 'abc' so -9; backwards all so -1.
>>> l[-2:-9:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

# For the same 'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
>>> l[7:0:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']
Comment

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)
Comment

PREVIOUS NEXT
Code Example
Python :: logger 
Python :: why is c++ faster than python 
Python :: drop row pandas column value not a number 
Python :: How to use Counter() Function 
Python :: make Python class serializable 
Python :: upload file setup django url 
Python :: remove whitespace method 
Python :: time a function python 
Python :: giving number of letter in python 
Python :: what does the .item() do in python 
Python :: loading bar in python 
Python :: how to implement dfa in python 
Python :: comments in python 
Python :: flask form options 
Python :: python print not working 
Python :: pandas filter 
Python :: numpy linspace function 
Python :: python pandas how to access a column 
Python :: pandas take entries from other column if column is nan 
Python :: python calling method from constructor 
Python :: datetime to string 
Python :: python minimum 
Python :: string comparison in python 
Python :: python looping through a list 
Python :: python new 
Python :: subarrays in python 
Python :: cmap seaborn 
Python :: python using shutil method 
Python :: split dataframe row on delimiter python 
Python :: comparison python 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =