Group multiple occurrences of array elements ordered by first occurrence in Python

In this tutorial, we will learn how to group multiple occurrences of array elements. We order them by their first occurrences. We will implement this in Python.

To do so, we require the use of dictionaries in Python. We use a dictionary to keep track of the number of occurrences of each element. This helps us to obtain the required output with low time complexity.

Dictionaries in Python

To implement this solution we need to have a basic understanding of dictionaries in Python.

Put simply, dictionaries are collections of pairs of values. Each pair consists of a ‘key’ and a ‘value’. Let’s take a look at an example.

`d = {1: "word", 'pi': 3.1415, '1 million': "10 Lakh"}`

Here, ‘d’ is a Python dictionary.

In this dictionary, the keys are
1, ‘pi’, and ‘1 million’

The values associated with each of these keys respectively are
‘word’, 3.1415, and ’10 lakh’.

Also, we see that the keys and values of a dictionary may be of different data types (int, string, etc).

We wish to store the number of occurrences of elements present in a list. Let’s say our list is as follows.

`[47.4, 'cat', 2.55, '13.67', 2.23, 32.234, 'cat', 2, 47.4, 13.67]`

Our dictionary stores the required information as shown below.

`{47.4: 2, 'cat': 2, 2.55: 1, '13.67': 1, 2.23: 1, 32.234: 1, 2: 1, 13.67: 1}`

Thus, we can use this information to create a new list of ordered groups.

get() for dictionaries in Python

Let us consider the dictionary ‘d’ again.

`d = {1: "word", 'pi': 3.1415, '1 million': "10 Lakh"}`

We can get the value associated with a key by simply using []. This is shown below.

`print(d['pi'])`

gives the output

`3.1415`

However, if we use the same operation for a non-existing key, we get an error as shown below.

`print(d[0])`

gives the output

`KeyError: 0`

The get() function returns the value associated with the key if it is present. If not, it returns the parameter passed. If no such parameter is passed, it returns None by default.

```print(d.get('pi', 8923))
print(d.get(2, 0))
print(d.get(23))```

gives the output

```3.1415
0
None```

Python program: Group multiple occurrences of array elements ordered by the first occurrence

Having understood these concepts, the code for the implementation is straightforward.

```# sample list of elements
collection = [47.4, 'cat', 2.55, '13.67', 2.23, 32.234,
'cat', 2.55, 47.4, 13.67]

# create an empty dictionary to hold the count
# of elements in the list
count = {}

# getting the counts of the elements.
# if the element is not present in count,
# we return 0
for i in collection:
count[i] = count.get(i, 0) + 1

# creating a new ordered list
ordered_collection = [i for i in count for j in range(count.get(i))]
print(ordered_collection)
```

Output

`[47.4, 47.4, 'cat', 'cat', 2.55, 2.55, '13.67', 2.23, 32.234, 13.67]`

Conclusion

In this tutorial, we learned how to group multiple occurrences of array elements ordered by their first occurrences in Python. We do this with the help of a dictionary to store the number of occurrences of each element.