Q:

Enter data in dictionary from two arrays

I have the following two arrays which I want to merge in a dictionary:
# Input:
wrd = ['i', 'am', 'am', 'the', 'boss', 'the', 'tiger', 'eats', 'rice', 'eats', 'grass']
cnt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Output: 
{
    'i': [0],
    'am': [1,2],
    'the': [3,5],
    'boss': [4],
    'tiger': [6],
    'eats': [7,9],
    'rice': [8],
    'grass': [10]
}

I have tried the following code, but I think my idea is leading me wrong way:
    dict={}
    j=0
    for i in wrd:
        if i in dict:
           dict[i].insert(cnt[j])
        else:
           dict[i].append(cnt[j])
           j+=1

    v=dict.values()
    k=dict.keys()
    for k,v in dict.items():
         print k,v

As I am a newbie in python, I acknowledge my lack of knowledge here. 

A:

Use collections.defaultdict here. See the snippet
>>> wrd=['i','am','am','the','boss','the','tiger','eats','rice','eats','grass']
>>> cnt=[0,1,2,3,4,5,6,7,8,9,10]
>>> from collections import defaultdict
>>> a = defaultdict(list)
>>> for key, val in zip(wrd, cnt): # Preferably for val, key in enumerate(wrd):
        a[key].append(val)

>>> a
defaultdict(<type 'list'>, {'grass': [10], 'i': [0], 'am': [1, 2], 'eats': [7, 9], 'boss': [4], 'tiger': [6], 'the': [3, 5], 'rice': [8]})
>>> a['am']
[1, 2]
>>> a['the']
[3, 5]

A:

Using the right tool for the job (here, defaultdict, as in Sukrit Kalra's answer) is always the best solution. But it's also useful to understand what's wrong with your attempt.
if i in dict:
   dict[i].insert(cnt[j])
else:
   dict[i].append(cnt[j])
   j+=1

If i is already in the dict, that's fine: dict[i] is a list, and you're going to call insert on it. That's not going to work, but only because insert requires two parameters—the index to insert the object, as well as the object to insert. Just change that to append(cnt[j]), or insert(0, cnt[j]), or whatever is appropriate.
But if i is not already in the dict, you're trying to append to something that doesn't exist. That obviously won't work. You have to create a list and put it in dict[i] before you can do anything to dict[i]. So, you can just change that line to:
    dict[i] = [cnt[j]]

… and that will solve it.
And once you understand that, you can hopefully understand why Sukrit Kalra's answer is so cool: a defaultdict is just a dict that automatically creates a default value for any keys that aren't present. So, you can just write dict[i].append(cnt[j]) and it will work whether dict[i] already existed or not.

As a side note, naming a dict dict is a bad idea, because that hides the builtin class and constructor of the same name.
More generally, it always helps to use better names. The keystrokes you save with your cryptic abbreviations and one-letter names will be more than canceled out by the keystrokes you waste debugging your code and explaining it to people who you need to ask for help. Call the input something like words and counts, the outer loop variable word, the j counter something like count_index, etc.
Meanwhile: cnt is almost completely useless. For any number up to 10, cnt[j] is just j, and for any number above 10, it's an IndexError. Why not just use j?

