PYTHON] How to start in the middle, not the first time, when operating a for door with a list or a panda?

Asked 2 years ago, Updated 2 years ago, 94 views

Hello, I'm a beginner at PYTHON. So please understand that the level of questioning can be very low.

foriiin list variable:
    print(ii)

When you run with , the list variable always starts from the beginning. But sometimes you have to start in the middle. How can I start in the middle at this time?

Thank you.

python for

2022-09-20 22:15

2 Answers

Well, list variables always start from scratch. But sometimes you have to start in the middle.

First of all, I understood what he said. Please tell me if it's wrong.


myitem = [1,2,3,4,5,6,7,8,9]

#print all item.
for i in myitem:
    print(i)

#print item from 3rd from myitem.
for i in range(2,len(myitem)):
    print(myitem[i])

If you look at it in a way, it's a very C-like way, and it's simple and intuitive Like a programmer? I think that's the way.

If you were to do it in a Python way?

myitem = [1,2,3,4,5,6,7,8,9]

for i in myitem[3:]:
    print(i)

I think I can do this.


2022-09-20 22:15

myitems = [1,2,3,4,5,6,7,8,9]

#print item from 3rd from myitem.
for i, item in enumerate(myitems):
    if i < 2: 
        continue
    print(item)

Let's also find a way to use enumerate, which is often used when Python needs list indexes.


2022-09-20 22:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.