Python Forum
Adding to the dictionary inside the for-loop - weird behaviour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding to the dictionary inside the for-loop - weird behaviour
#2
It's the line :

dict_words = dict.fromkeys(list1, [])
That's causing the trouble. I'm not sure how you would use the method fromkeys differently in order to correct the problem but if you replace that line with :
dict_words = {'!,': [], 'Hey!': []}
then your code will produce the expected results.
InputOutput007 likes this post
Reply


Messages In This Thread
RE: Adding to the dictionary inside the for-loop - weird behaviour - by BashBedlam - Jan-20-2021, 09:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 484 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,930 Nov-07-2023, 09:49 AM
Last Post: buran
  logger behaviour setdetnet 1 925 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 760 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,308 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,816 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Help adding a loop inside a loop Extra 31 4,765 Oct-23-2022, 12:16 AM
Last Post: Extra
  Generator behaviour bla123bla 2 1,146 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,348 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  Inconsistent behaviour in output - web scraping Steve 6 2,627 Sep-20-2021, 01:54 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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