Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use for without in
#1
Hi,

Are there situations where one can use a for without an in?

Usually the for loop is

for x in whatever:
.....

I get the impression that "for" must always be used with the "in" keyword. Is this true?

Can anyone give me an example of for loop where the "for" is used without an accompanying "in" keyword?

Thanks for any replies
Reply
#2
According to python docs:
Quote: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))
...
cat 3
window 6
defenestrate 12
Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection:
# Strategy:  Iterate over a copy
for user, status in users.copy().items():
    if status == 'inactive':
        del users[user]

# Strategy:  Create a new collection
active_users = {}
for user, status in users.items():
    if status == 'active':
        active_users[user] = status
Then, there is also in range which you can use.Otherwise, I haven't seen a for loop being used without in or in range, so I don't think it is possible. However, there may be such instances, but I hardly think its is possible. You can go through this, if you want to see more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
It doesn't make sense to consider range as part of the syntax of the for loop, because it's basically a function that returns an iterable, so a call to it can be replaced with any iterable.

Yes, in is part of the syntax for the for loop. The specification for the for can be found in the language reference, which presents the grammar of the language in a syntax called BNF (see section 1.2 Notation for details about that).
Reply
#4
No, I just meant there is in and in range are the 2 main things used after the for i part
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
Thank you. It's now clear.
Reply
#6
So when "in" is used in a for, the "in" is not the in operator that gives a True or False value.

Am I correct?
Reply
#7
The operator in has two meanings:
  • To check if an object is in a container. A boolean object is returned.
  • Named assignment with a for-loop.

Containment checking:
"foo" in [] # list
"foo" in {} # dict
"foo" in () # tuple
"foo" in set()
All this evaluations return False because all containers are empty and do not hold a reference to "foo".


Named Assignment:
for a in range(5):
    print(a)
Each object from the range-object is assigned to the name a.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020