Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent syntax regarding List returns List objects with non-similar list elements. #74323

Closed
AkshayDeogaonkar mannequin opened this issue Apr 22, 2017 · 2 comments
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@AkshayDeogaonkar
Copy link
Mannequin

AkshayDeogaonkar mannequin commented Apr 22, 2017

BPO 30137
Nosy @stevendaprano

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-04-22.18:16:43.046>
created_at = <Date 2017-04-22.14:01:58.944>
labels = ['type-bug', 'invalid']
title = 'Equivalent syntax regarding List returns List objects with non-similar list elements.'
updated_at = <Date 2017-04-22.18:16:43.044>
user = 'https://bugs.python.org/AkshayDeogaonkar'

bugs.python.org fields:

activity = <Date 2017-04-22.18:16:43.044>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2017-04-22.18:16:43.046>
closer = 'steven.daprano'
components = []
creation = <Date 2017-04-22.14:01:58.944>
creator = 'Akshay Deogaonkar'
dependencies = []
files = []
hgrepos = []
issue_num = 30137
keywords = []
message_count = 2.0
messages = ['292120', '292132']
nosy_count = 2.0
nosy_names = ['steven.daprano', 'Akshay Deogaonkar']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue30137'
versions = ['Python 3.6']

@AkshayDeogaonkar
Copy link
Mannequin Author

AkshayDeogaonkar mannequin commented Apr 22, 2017

lst = [0,1,2,3,4]
print(lst[0:3]) #returns [0,1,2]
print(lst[:3]) #returns [0,1,2]

#Above two syntax returns same lists.

print(lst[0:3:-1]) #returns []
print(lst[:3:-1]) #returns [4]

#Here is a bug; what expected was that the both syntax would have
returned the similar lists; however, they didn't!

@AkshayDeogaonkar AkshayDeogaonkar mannequin added the type-bug An unexpected behavior, bug, or error label Apr 22, 2017
@stevendaprano
Copy link
Member

The behaviour is as documented and is not a bug. When you have a three-argument extended slice, the starting and stopping values depend on whether the stride (step) is positive or negative. Although that's buried in a footnote to the table.

https://docs.python.org/3/library/stdtypes.html#common-sequence-operations

The current behaviour is necessary so that the common case of both start and stop being blank is supported correctly for negative stride:

py> "abcde"[::-1]
'edcba'

So with a positive stride, your first example lst[0:3:-1] starts at index 0, ends at index 3, with step -1. That is an empty slice.

But your second example lst[:3:-1] starts at the end of the list, index len(lst), ends at 3, with step -1. That is equivalent to lst[5:3:-1] which is not the same as your first example.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant