How to make a dictionary in python and save data in a file : <p>I am iterating over grouped items and based on item,user combination I am extracting user features, by using following code:</p>

<pre><code>for counter in grouped.iterrows():
    user_id = counter[1]['user_id']
    item_id = counter[1]['item_id']
    career_level = get_value(users, user_id, 'career_level')
    industry_id = get_value(users, user_id, 'industry_id')
    country = get_value(users, user_id, 'country')
    career_level = 'CL_' + str(career_level)
    industry_id = 'IND_' + str(industry_id)
    print(item_id, user_id, country, career_level, industry_id)
</code></pre>

<p>In output what I got is:</p>

<pre><code>5 797978 JO_4092133 ch CL_0 IND_1
12 1524899 JO_524518 JO_2169794 JO_2905196 de CL_2 IND_0
12 2703661 JO_1210814 JO_2573697 de CL_3 IND_0
14 1054241 JO_2804344 JO_1072229 de CL_3 IND_14
14 1297953 JO_3482421 de CL_6 IND_0
14 1548532 JO_425546 de CL_2 IND_0
14 1609264 JO_4438218 JO_1151866 de CL_3 IND_9
</code></pre>

<p>Now my desired output is something like this:</p>

<pre><code>5 797978 JO_4092133 ch CL_0 IND_1

12 1524899 JO_524518 JO_2169794 JO_2905196 de CL_2 IND_0, 2703661 JO_1210814 JO_2573697 de CL_3 IND_0

14 1054241 JO_2804344 JO_1072229 de CL_3 IND_14, 1297953 JO_3482421 de CL_6 IND_0, 1548532 JO_425546 de CL_2 IND_0, 1609264 JO_4438218 JO_1151866 de CL_3 IND_9
</code></pre>

<p>That means if some user1 has interacted with item1 and another user2 has also interacted with item1, then user1 and user2's features should be in single row. </p>

<p>Could anyone suggest me how could I achieve this goal?</p>

<p>My second question is:
How can I write this data into a file?</p>

<p>I am a beginner to python. I appreciate your help.
Thanks</p>
