#🔒 List Slicing (Anybody know how to fix this issue????)

31 messages · Page 1 of 1 (latest)

young bane
#
tours_data = []
for token in input().split():
    tours_data.append(int(token))

slice_size = len(tours_data)//4
spring_tours = tours_data[-8:2]
summer_tours = tours_data[-6:4]
fall_tours = tours_data[-4:6]
winter_tours = tours_data[-2:]


print(f'Number of tour dates in each season: {slice_size}')
print(f'All year tours: {tours_data}')
print(f'Spring tours: {spring_tours}')
print(f'Summer tours: {summer_tours}')
print(f'Fall tours: {fall_tours}')
print(f'Winter tours: {winter_tours}')```
calm adderBOT
#

@young bane

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

red berry
#

negative slices refer to the end of the list

#

ie. -4 means go 4 back from wherever the end of the list is

young bane
#

The program I'm working on gives me multiple inputs

young bane
kindred grove
#

!slice

calm adderBOT
#
Sequence slicing

Slicing is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards.

Examples

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:]  # from element 2 to the end
['c', 'd', 'e', 'f', 'g']
>>> letters[:4]  # up to element 4
['a', 'b', 'c', 'd']
>>> letters[3:5]  # elements 3 and 4 -- the right bound is not included
['d', 'e']
>>> letters[2:-1:2]  # Every other element between 2 and the last
['c', 'e']
>>> letters[::-1]  # The whole list in reverse
['g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> words = "Hello world!"
>>> words[2:7]  # Strings are also sequences
"llo w"
young bane
#

my question was confusing lmao im trying to find a better way to ask it

#

The program I'm working with takes in an input so any input can be given

red berry
#

yes

#

what im saying is you're slicing the list backwards

young bane
#

but if I sliced from index[0] would it work with all the inputs?

red berry
#

this prints an empty list because you're going from the 4th element from the end (ie. 7) to the 1st element from the start (ie. 1) ```py
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(L[-4 : 1])

young bane
#

yeah that makes sense

#

wait

red berry
#

(memory wise)

young bane
#
!e
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(L[-4 : 1])```
#

!e py L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(L[-4 : 1])

calm adderBOT
#

@young bane :white_check_mark: Your 3.12 eval job has completed with return code 0.

[]
young bane
#

no yeah that makes sense

#

I think I see what you mean

red berry
flint lotus
#

you’re hardcoding your solution to work on slice sizes of 2

#

you compute slice_size but then you don’t use it anywhere, and you do indexing like [-2:]

calm adderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.