The idea is simple.

- Build a dictionary of items and their frequencies.
- Create a list of sorted tuples of items by value from the dictionary.
- Return a list of k length from the sorted list.

```
dictionary = {}
for i in nums:
if i in dictionary:
dictionary[i] += 1
else:
dictionary[i] = 1
sorted_x = sorted(dictionary.items(), key=operator.itemgetter(1), reverse=True)
return [x[0] for x in sorted_x][:k]
```