简体   繁体   中英

For loop iterating through the list

I am learning the python from the Codeacademy website and I came across the loops section which is a little vague and hard to me. When the website wants to explain how does a for loop works, it gets help from lists. Like so:

for i in list34:
    #Some codes

The website says that when you run a for loop statement for a list, the for loop would iterate through the elements of the list then save them in i variable.

I just don't get the iterating through concept!

What does it mean?

for i in list34:
    #Some codes

This snippet will go through all the items of list34 (ie, iterate through them). In each iteration ("step" of the loop), i will be assigned the next value from the list, so your code could do something with it (eg, print it out).

Maybe some code example will help!

>>> li = [4,3,1,2,0]
>>> for x in li:
...     print(x)
... 
4
3
1
2
0
>>> 

What the for loop does is, it takes one item in the list at a time and assigns that item to the variable x . As the for loop takes items of lists one by one , it is called iterating through/on the list.

iterating over a list, or any data structure for that matter, means that it just takes every element, one after the other, from the given structure an does something with it.

in this case you have your i elements and you do stuff with them inside the for loop. the for statement makes sure, that every element of the list is handled.

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python's for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):

# Measure some strings:
words = ['cat', 'window', 'defenestrate']
for w in words:
    print w, len(w)

If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:

for w in words[:]:  # Loop over a slice copy of the entire list.
    if len(w) > 6:
        words.insert(0, w)

words

If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates lists containing arithmetic progressions:

range(10)

The given end point is never part of the generated list; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the 'step'):

range(5, 10)    
range(0, 10, 3)
range(-10, -100, -30)

To iterate over the indices of a sequence, you can combine range() and len() as follows:

a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print i, a[i]

Read More: docs.python.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM