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
#1
dic = {3: 0.1, 4: 0.2, 5: 0.1, 6:0.2, 7:0.1, 8:0.2}
ls =  {0.2,   0.1,     0.2,    0.1,   0.2,   0.1}
n = {}


for i, v, l in list(dic.keys()),list(dic.values()),ls:

   add = v + l
   n[i] = add

# expected result {3: 0.3, 4: 0.3, 5: 0.3, 6:0.3, 7:0.3, 8:0.3}
print (n)
I'm trying to use keys, values of dic and entries of ls at the same time, can I do that?
 I'm not sure why it's not correct ?

Wait, I can only have one element after in ?
Reply
#2
for i, k in enumerate(dic.keys()):
    n[k] = dic[k] + ls[i]
Did you try your code? What does happen when you run it?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
#4
You're right. 
In this case enumerate(sorted(dic.keys())) would be enough.
But also, the ls is not a list
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
the error message is like too many things to unpack
Sorry should have used [] for list,
Oh, this seems cute, so the enumerate method turns the keys into something like [0: key0, 1: key1, 2: key2...]?
Reply
#6
https://docs.python.org/2/library/functi...#enumerate

It adds a counter to an iterable object. 

In [1]: for counter, iterable in enumerate(range(5)):
   ...:     print(counter, iterable)
   ...:        
0 0
1 1
2 2
3 3
4 4
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 548 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 672 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 489 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 1,002 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,299 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  check if element is in a list in a dictionary value ambrozote 4 1,966 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,960 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,580 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,600 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,256 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