Ruan Bekker's Blog

From a Curious mind to Posts on Github

Using Python to Build a Dictionary From Data Eg Sports Per Person

I had to achieve a way to provide data in key-value format, where I wanted to see what sports people like, eg: {"ruan": ["rugby", "cricket"]}

The Idea

So my idea was to have the name as the key, and the sports as the value in a list.

Some Catches

So for this post, I will be setting the data statically in the code, while at the time I was working data that was returned via a API.

I am looping through each occurence, adding the name, and when the name exists, I append the sport to the list of the person.

The catch was that, if there was any duplicated data, the person will only exists once in the dictionary that I am building, but the sport will be appended, so if there were 2 occurences of rugby it will show the sport 2 times. So I had to put some logic into the code to handle that.

The Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""|Info:

Printing Sports per Person, by looping through data, appending the sports to a list per person, which gets added to our dictionary.

Variables:
 group {dict} -- "the dictionary that we are building up"
 people {list} -- "list of people with their sport choices"
 for sportman in people: {[for-loop]} -- "iterating through our data, if the sport exists, continue, if not, apeend it to the list"
 print(group) {[dict]} -- "printing the results"
"""

group = {}
people = [
  {
      "name": "ruan",
      "sport": "cricket"
  },
  {
      "name": "stefan",
      "sport": "rugby"
  },
  {
      "name": "stefan",
      "sport": "cricket"
  },
  {
      "name": "james",
      "sport": "rugby"
  },
  {
      "name": "james",
      "sport": "golf"
  },
  {
      "name": "stefan",
      "sport": "rugby"
  },
  {
      "name": "james",
      "sport": "hockey"
  }
]

for sportman in people:
    if sportman['name'] in group:
        if sportman['sport'] not in group[sportman['name']]:
            group[sportman['name']].append(sportman['sport'])
        else:
            pass
    else:
        group[sportman['name']] = []
        group[sportman['name']].append(sportman['sport'])

print(group)

Running the Script:

When running the script results in the following:

1
2
$ python sports.py
{'james': ['rugby', 'golf', 'hockey'], 'ruan': ['cricket'], 'stefan': ['rugby', 'cricket']}