2

How do i add key,value to a dictionary in python? I defined an empty dictionary and now I want to pass a bunch of keys from a list and set their value as 1. From what I did it creates me every iteration a new dictionary, but I want to append the key,value so eventually I will recieve only one dictionary. this is my code:

def count_d(words):
    count_dict={} 
    words_set= set(words)
    for i in words_set:
        count_dict[i]= 1
        print (count_dict)
ford prefect
  • 7,096
  • 11
  • 56
  • 83
user2751595
  • 337
  • 3
  • 13
  • 1
    "But I want to append the key,value so eventually I will recieve only one dictionary" -- Can you explain? The code you have only creates a single dictionary. Perhaps you want to `return count_dict` after the for loop has been completed? – mgilson Dec 20 '13 at 19:30

3 Answers3

4

What you are looking for is dict.fromkeys along with dict.update.

From the docs

Create a new dictionary with keys from seq and values set to value.

Example Usage

>>> d = {}
>>> d.update(dict.fromkeys(range(10), 1))
>>> d
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}

Note, you need an dict.update along with dict.fromkeys, so as to update the dictionary in-place

Instead, if you want to create a dictionary and assign use the notation

>>> d = dict.fromkeys(range(10), 1)
>>> d
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}  

Unfortunately, for in-place update, you need to create a throw-away dictionary before passing to the dict.update method. A non-intuitive method is to leverage itertools

from itertools import izip, repeat
d.update(izip(xrange(10), repeat(1)))

The same idea can be extended to OrderedDict which is often used, as an alternate for OrderedSet as standard library does not provide one.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
Abhijit
  • 62,056
  • 18
  • 131
  • 204
2

the fromkeys method does what you want:

count_dict = dict.fromkeys(words_set, 1)

This gives count_dict it's keys from words_set, each with value of 1.

More info on fromkeys here

Totem
  • 7,189
  • 5
  • 39
  • 66
  • No it doesn't. `fromkeys` does not mutate `count_dict`, it returns a new dict. Plus, it's a class method, not an instance method, so there is no reason to create a new dict just to call it on. – abarnert Dec 20 '13 at 19:33
  • Apologies and thanks, I have edited the post – Totem Dec 20 '13 at 19:35
1

How do i add key,value to a dictionary in python?

Exactly as you are:

count_dict[i] = 1

From what I did it creates me every iteration a new dictionary

No it doesn't. It keeps appending to the same iteration. The problem is that you print the dictionary-so-far on every iteration.

So, you were very close to what you wanted. I believe that all you want to do is unindent the print statement, so it only happens once at the end, instead of each time through the loop:

def count_d(words):
    count_dict={} 
    words_set= set(words)
    for i in words_set:
        count_dict[i]= 1
    print (count_dict)

Of course it will probably be a lot more useful to return count_dict, not print it. And Abhijit's answer shows a much simpler way to do this. (Another simple way to do it: count_dict = dict(collections.Counter(words_set)). But I think dict.from_keys is more meaningful in this case, despite your variable names.)

But I think this is the part you were actually struggling with.

abarnert
  • 354,177
  • 51
  • 601
  • 671