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 :: double underscore methods python 
Python :: python poetry 
Python :: atoi in python code 
Python :: Implement a binary search of a sorted array of integers Using pseudo-code. 
Python :: ++ in python 
Python :: django make new application folder 
Python :: class decorator python 
Python :: global var in python 
Python :: time a function python 
Python :: how to loop through an array in python 
Python :: Python DateTime Date Class Syntax 
Python :: round python print 
Python :: plt.hist bins 
Python :: create django object 
Python :: python interview questions and answers pdf 
Python :: simple python program for beginners 
Python :: clear 
Python :: for loop in python 
Python :: python floor function 
Python :: interviewbit with Python questions solutions 
Python :: how to standardize the image data to have values between 0 and 1 
Python :: how to add elements in a list together python 
Python :: import os in python 
Python :: Syntax of Python Frozenset 
Python :: Python NumPy ndarray flatten Function Syntax 
Python :: create a new column in pandas dataframe based on the existing columns 
Python :: how to create list in python 
Python :: whitespace delimiter python 
Python :: python clear() 
Python :: flask page 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =