Python getting the unique and smallest value pairs from two lists with same length

python list
python set
python generate pairs from list
python pair elements in list
python pair two lists
python all pairs from two lists
find pairs in list python
list comprehension || python

I have two lists:

index = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
value = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]

Same length but what I want in return are unique number from index list and smallest value of the value list according to the index

Result should be like:

index_result = [1,2,3,4,5,6,7,8,9,10]

value_result = [1,2,2,1,2,2,2,2,1,1]

I have tried:

index = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
value = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]
index_result = []
value_result = []
#global small_value
j = 0
while j < len(index):
    if j == 0:
        try:
            if index[j] == index[j+1]:
                small_value = min(value[j],value[j+1])
            elif index[j] != index[j+1]:
                index_result.append(index[j])
                value_result.append(value[j])
        except IndexError as e:
            print(e)
            pass
        j = j + 1
        print('small value is for index j ==0')
        print(small_value)
    elif j <len(index) - 1:
        try:
            # if index[j] == index[j-1]:
            #     small_value = min(value[j],value[j-1])
            if index[j] != index[j+1] and index[j] != index[j-1]:
                index_result.append(index[j])
                value_result.append(value[j])
            elif index[j] != index[j+1] and index[j] == index[j-1]:
                index_result.append(index[j])
                value_result.append(small_value)
        except IndexError as e:
            print(e)
            pass
        j = j + 1
        print('small value is for index 0 < j <len(index)')
        print(small_value)
    elif j == len(index) - 1:
        try:
            if index[j] == index[j-1]:
                small_value = min(value[j],value[j-1])
                index_result.append((index[j]))
                value_result.append(small_value)
            elif index[j] != index[j-1]:
                index_result.append(index[j])
                value_result.append(value[j])
        except IndexError as e:
            print(e)
            pass
        j = j + 1
        print('small value is for j = len(index) - 1')
        print(small_value)

print (index_result)
print (value_result)

The result is close to what expect but still wrong:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 2, 2, 1, 2, 2, 2, 2, 1, 1]


Since you tag pandas

pd.DataFrame([index,value]).T.sort_values([0,1]).drop_duplicates(0)
     0  1
3    1  1
4    2  2
8    3  2
9    4  1
11   5  2
13   6  2
14   7  2
15   8  2
16   9  1
18  10  1

Python, Python | Unique pairs in list · Python - Product of consecutive pairs in list · Python - Extract Similar pairs from List. everythingispossible. Check out this  If all elements are equal and the length of the lists are the same, the return value is True. Problem : Given are two lists l1 and l2 . You want to perform Boolean Comparison : Compare the lists element-wise and return True if your comparison metric returns True for all pairs of elements, and otherwise False .


I think this is what you wanted to achieve

import math

indices = [1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10]
values = [2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11]

result = [math.inf] * max(indices)

for i, v in zip(indices, values):
    result[i-1] = min(result[i-1], v) # Apparently 1 based indexing

print(result)

[1, 2, 2, 1, 2, 2, 2, 2, 1, 1]

Python, Given a list, print all the unique numbers in any order. This is done using one for loop and other if statement which check if the value is Python program to check if two Using set() property of Python, we can easily check for the unique values. last_page Python List Comprehension to find pair with given sum from two  Using Python’s import numpy, the unique elements in the array are also obtained. In first step convert the list to x=numpy.array (list) and then use numpy.unique (x) function to get the unique values from the list. numpy.unique () returns only the unique values in the list.


If your index is monotonic-increasing as in the sample, you may try this python solution using itertools.groupby

from itertools import groupby

d = {k: min([x[1] for x in g]) for k, g in groupby(zip(index, value), 
                                                     lambda x: x[0])}

In [95]: d
Out[95]: {1: 1, 2: 2, 3: 2, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2, 9: 1, 10: 1}

index_result = list(d.keys())

Out[103]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

value_result = list(d.values())

Out[105]: [1, 2, 2, 1, 2, 2, 2, 2, 1, 1]

Does the zip function work with lists of different lengths?, Answer The zip() function will only iterate over the smallest list Do the lists passed to the zip() function have to be the same length? It is possible to use the cycle() function from itertools to repeat values from the shorter list. Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN) Count of distinct sums that can be obtained by adding prime numbers from given arrays; Find the maximum cost of an array of pairs choosing at most K pairs; Find the smallest and second smallest elements in an array; Count Pairs from two arrays with even


Numpy solution:

index = np.array([1,1,1,1,2,2,2,2,3,4,5,5,5,6,7,8,9,10,10,10])
value = np.array([2,3,2,1,2,4,6,8,2,1,5,2,7,2,2,2,1,55,1,11])

[value[index == i].min() for i in np.unique(index)]
# [1, 2, 2, 1, 2, 2, 2, 2, 1, 1]

How to use Python's min() and max() with nested lists – dbader.org, Let's talk about using Python's min and max functions on a list containing other lists. example to work with, let's say we have the following list of item, weight pairs: But if we simply call min and max on that nested list we don't get the results this determines the outcome of the comparison; if they are equal, the next two  Python Program to find the Smallest Number in a List Example 2. This python program is the same as above. But this time, we are allowing the user to enter the length of a List.


18 Most Common Python List Questions, Lists are one of the four built-in data structures in Python, together with tuples, dictionaries, and sets. Sets require your items to be unique and immutable. You can pass your list to the len() function to get the length of your list back. There are two very simple ways to get the values in your lists sorted in  Python Nested List Length Example. Let us see how to find the length of a nested list. For this, we declared a list inside a list with the combination of a few other items.


5. Data Structures, Remove the first item from the list whose value is x. For example, this listcomp combines the elements of two lists if they are not equal: Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4: of key: value pairs, with the requirement that the keys are unique (within one  Varun August 19, 2019 Pandas : Get unique values in columns of a Dataframe in Python 2019-08-19T08:09:44+05:30 Pandas, Python No Comment In this article we will discuss how to find unique elements in a single, multiple or each column of a dataframe.


Tuples, Syntactically, a tuple is a comma-separated list of values: The second loop traverses the list of tuples and builds a list of words in descending order of length. One of the unique syntactic features of the Python language is the ability to In this example we have a two element list (which is a sequence) and assign the first  Pandas library in Python easily let you find the unique values. In this tutorial, we will see examples of getting unique values of a column using two Pandas functions. We will first use Pandas unique() function to get unique values of a column and then use Pandas drop_duplicates() function to get unique values of a column.