Reputation:
I am trying to put them into three groups of two people each. I'm not sure where I need to go from here. I don't know how I can add two people to a group efficiently.
so it should look like this
group_one = {'2': dan, '8': tom}
group_two = {'10': james, '12': emily}
group_three = {'7': kim , '13': jones}
Upvotes: 0
Views: 45
Reputation: 2401
@Sacul code is fine. But if you want an even smaller solution do:
>>> my_list = [['dan',2],['tom',8],['james',10],['emily',12],['kim',7],['jones',13]]
>>> teams = [{i[1] : i[0] for i in my_list[n:n+2]} for n in range(0, len(my_list), 2)]
[{2: 'dan', 8: 'tom'}, {10: 'james', 12: 'emily'}, {7: 'kim', 13: 'jones'}]
Now your teams are split and stored on a list of dictionaries.
To have them on different variables like group_one
, group_two
and group_three
use unpacking.
>>> group_one, group_two, group_three = teams # Or simply use the code of teams
Upvotes: 1
Reputation: 51395
You could slice your list within dict comprehension to create each variable:
group_one = {i[1]:i[0] for i in my_list[0:2]}
group_two = {i[1]:i[0] for i in my_list[2:4]}
group_three = {i[1]:i[0] for i in my_list[4:6]}
>>> group_one
{2: 'dan', 8: 'tom'}
>>> group_two
{10: 'james', 12: 'emily'}
>>> group_three
{7: 'kim', 13: 'jones'}
Upvotes: 2