Python Forum
having dictionary and list to iterate in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
having dictionary and list to iterate in for loop
#3
(Jan-04-2017, 08:16 AM)wavic Wrote:
 for i, k in enumerate(dic.keys()):     
    dic[k] = dic[k] + ls[i] 
As you can't rely on the order of the dict, this is not guaranteed to give the desired result.
You would need to sort the keys to guarantee behavior with this method.
Honestly if you want to add values from a sequence where the values correspond to keys it would make more sense if both were dicts.
Reply


Messages In This Thread
RE: having dictionary and list to iterate in for loop - by Mekire - Jan-04-2017, 08:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 634 Apr-29-2024, 04:38 PM
Last Post: Calab
  Dictionary in a list bashage 2 602 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 748 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  How to add list to dictionary? Kull_Khan 3 1,045 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,337 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  check if element is in a list in a dictionary value ambrozote 4 2,021 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 2,048 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,622 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,680 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,326 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

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