# 5 Best Ways to Write a Python Program to Print Numeric Index Array with Sorted Distinct Values

Rate this post

π‘ Problem Formulation: Python programmers often need to handle series or lists of data with redundant values. Our goal is to create a Python program that takes a series of numbers, filters out the duplicates, sorts the remaining values, and prints them alongside their numeric index in the form of an array. If given the series [5, 3, 1, 5, 2, 3, 4], the expected output would be a printout of the index-value pairs for the sorted distinct values, such as [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)].

## Method 1: Using Set with Sorted

Set is a built-in Python data type that automatically removes duplicate values. We can combine the set with the sorted function to generate a list of distinct sorted values from a given series. This method is simple, direct, and efficient for most cases.

Here’s an example:

```series = [5, 3, 1, 5, 2, 3, 4]
distinct_sorted = sorted(set(series))
index_array = list(enumerate(distinct_sorted))
print(index_array)```

Output:

`[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]`

This code snippet converts the series into a set to remove duplicates, sorts it, and then enumerates the list to pair each element with its index. The resulting index-array pairs are printed to show the distinct, sorted values along with their numeric indices.

## Method 2: List Comprehension with Sorting

List comprehensions offer a compact way to apply operations to each element in a list. By combining a list comprehension to identify unique elements and then sorting them, we get an elegant one-liner solution.

Here’s an example:

```series = [5, 3, 1, 5, 2, 3, 4]
distinct_sorted = sorted({v: None for v in series})
index_array = [(i, val) for i, val in enumerate(distinct_sorted)]
print(index_array)```

Output:

`[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]`

This approach uses dictionary comprehension to first remove duplicates (since dictionary keys must be unique) and then sorts the keys. List comprehension is then used to enumerate and print the index-value pairs.

## Method 3: Using itertools and operator

For those preferring a functional programming approach, the itertools and operator modules from Python’s standard library can be used in combination to achieve the result.

Here’s an example:

```from itertools import groupby
from operator import itemgetter

series = [5, 3, 1, 5, 2, 3, 4]
# Sort first to ensure groupby can group all occurrences of each number consecutively
series.sort()
# Use itemgetter to remove duplicates and keep the first one only
distinct_sorted = [k for k, g in groupby(series, key=itemgetter(0))]
index_array = list(enumerate(distinct_sorted))
print(index_array)```

Output:

`[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]`

The itertools.groupby() function groups adjacent elements in the sorted list, and the operator.itemgetter() function is used to extract the first unique values. Finally, enumerate is used to pair index-value and print.

## Method 4: Using numpy unique function

If working within a scientific or numerical context, numpy offers optimized and convenient functions such as unique to deal with array operations, including finding distinct values.

Here’s an example:

```import numpy as np

series = [5, 3, 1, 5, 2, 3, 4]
distinct_sorted = np.unique(series)
index_array = list(enumerate(distinct_sorted))
print(index_array)```

Output:

`[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]`

The numpy unique() function immediately provides sorted unique values from the input array. Enumerate is then used for matching with indices and the resulting list is printed.

## Bonus One-Liner Method 5: Using a Lambda with Sorted

A more concise alternative is to employ a lambda function within the sorted method to create the index value pairs directly, still ensuring uniqueness and order.

Here’s an example:

```series = [5, 3, 1, 5, 2, 3, 4]
index_array = sorted((i, val) for i, val in enumerate(sorted(set(series))))
print(index_array)```

Output:

`[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]`

This one-liner uses a generator expression with sorted and set to produce the distinct sorted values augmented with the indices, all in a single condensed line of code.

## Summary/Discussion

• Method 1: Using Set with Sorted. Simple and Pythonic. Can be somewhat inefficient with large series due to the conversion to and from a set.
• Method 2: List Comprehension with Sorting. Offers concise syntax. Relies on dictionary keys for uniqueness, which is less direct than set.
• Method 3: Using itertools and operator. Utilizes a functional approach, which can be less readable for some. Offers a different flavor of Python coding with standard library tools.
• Method 4: Using numpy unique function. Fast and efficient for large datasets. Requires numpy, which is an external library and may not be suitable for minimal dependency requirements.
• Method 5: Using a Lambda with Sorted. Concise one-liner. However, might be a bit harder to understand for beginners in Python.