#๐Ÿ”’ Is there an easier way to do this?

41 messages ยท Page 1 of 1 (latest)

mighty ridge
#

Given a list,
[101, 102, 103, 201, 202, 203]
How would you go through 3 items at a time, like 101, 102,103

fair flameBOT
#

@mighty ridge

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.

mighty ridge
#

Yes

fading pine
mighty ridge
#

Nooooo

#

It should be sliding 3 over

fading pine
#

u can configure size however u want

mighty ridge
#

so 1,15,1
then 2,6,12

fading pine
#

if u understand how it works with 4 elements u can just make it work for 3 with a adjustment of just a number

mighty ridge
#

the length is not my problem, it needs to be exclusive.

fading pine
#

anyways, there is tons of different ways to do this, how much python experience u got and do u know list slicing pithink

fading pine
mighty ridge
#

I got usual sliding window using this

#
for index,item in enumerate(colm1):
    try:
        print(item,colm1[index+1],colm1[index+2])
    except IndexError:
        pass
fading pine
#

usually using range is easier for these kind of needs

#

because we already doing everything based on index, so just makes life easier

mighty ridge
#
for i in range(0,len(colm1),3):
    print(colm1[i])
    print(colm1[i+1])
    print(colm1[i+2])
fading pine
#

!e

colm1 = [1,15,1,2,6,12]

for i in range(0,len(colm1),3):
    print(colm1[i])
    print(colm1[i+1])
    print(colm1[i+2])
    print()
fair flameBOT
fading pine
#

it works pithink

mighty ridge
#

yeah just wondering if theres an easier way

#

looks weirddd

fading pine
mighty ridge
#

show

fading pine
#

so

#

slicing uses same input format as range

#

start stop step

mighty ridge
#

oh wait i got it

fading pine
#

!e

colm1 = [1,15,1,2,6,12]

print(colm1[0:5:2])
fair flameBOT
mighty ridge
#
for i in range(0,len(colm1),3):
    print(colm1[i:i+3])
#

ok thanks, im kinda new

#

it just didn't come to mind

fading pine
fading pine
mighty ridge
#

.close

rocky crescentBOT
#
Did you mean:
mighty ridge
#

!close

fair flameBOT
#
Python help channel closed with !close

This help channel has been closed. 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.