Python Forum
How can this for loop work without : ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can this for loop work without : ?
#1
I am working my way through Reuven Lerner's book Python Workout (great book!).

On page 86 is a program to find the longest words.

I have 10 books from gutenberg.org as text files.

I changed the find_longest_word function so it would not return web addresses and other things as long words.

def find_longest_word(filename): def fakeWords(word): notWanted = ['/', '_', '-', '—', '(', '@', 'www', '—', '"', 'ï', '»'] for sign in notWanted: if sign in word: return True # first the longest word is empty longest_word = '' # check each line 1 line at a time for one_line in open(filename): # check each word for length for one_word in one_line.split(): if fakeWords(one_word): continue else: word = one_word.replace('"', '').replace(',', '').replace('.', '').replace('”', '') if len(word) > len(longest_word): longest_word = word return longest_word
The part that puzzles me is this main function (apparently this is called a dictionary comprehension):

# this works great, about 5 seconds for 10 books! def find_all_longest_words(dirname): return {filename: find_longest_word(os.path.join(dirname, filename)) for filename in os.listdir(dirname) if os.path.isfile(os.path.join(dirname, filename))}
Why doesn't the for loop, without : and no indent throw an error??

If I put : after the for loop declaration, then I get an error!
Reply
#2
It is called dictionary comprehension. Basically the code is roughly equivalent to:

def find_all_longest_words(dirname): dict = {} for filename in os.listdir(dirname): if os.path.isfile(os.path.join(dirname,filename)): dict.update({filename:find_longest_word(os.path.join(dirname,filename}) return dict
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Questions about while loop - why does one work, and the other variant does not Swgeek 2 1,466 Aug-29-2024, 07:57 AM
Last Post: Swgeek
  While Loop Does Not Work Properly mactron 4 2,637 Jun-22-2023, 01:04 AM
Last Post: mactron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 3,820 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Please help my while loop does not work as expected KingKhan248 6 4,654 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  For loop in my __init__ doesn't work as expected Jessy 2 3,556 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 3,686 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 4,512 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  for loop just work one Faruk 1 2,958 Jan-19-2019, 05:34 PM
Last Post: Larz60+
  'Looping' does not work out within a 'for Loop' Placebo 4 5,303 Sep-15-2018, 08:19 PM
Last Post: Placebo
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 3,223 Aug-25-2018, 10:44 AM
Last Post: j.crater

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.