# Comparing two lists and performing an operation in Python

I have two lists `I` and `i`. I want to find for each element of `i`, how many values are less than in `I` and add the total number of such values to the specific `i` element. For example, element `15` in `i` has two values less than itself in `I` i.e. `[8,11]`. So `2` should be added to `15` and the combination stored in `Values`. I present the expected output.

``````I = [8, 11, 19, 37, 40, 42]

i=[15, 17, 27, 28, 31, 41]
``````

The expected output is

``````New i=[17,19,30,31,34,46]
Values=[[8,11],[8,11],[8,11,19],[8,11,19],[8,11,19],[8,11,19,37,40]]
``````

### >Solution :

Assuming your list `I` is sorted, you can use `bisect_left` to get insertion point in your list `I` for each element in `i` and then slice the list. It uses binary search.

With that you can do:

``````from bisect import bisect_left
Values = [I[:bisect_left(I, e)] for e in i]
New_i =  [e + len(Values[j]) for j, e in enumerate(i)]
``````

print(Values):

``````[[8, 11], [8, 11], [8, 11, 19], [8, 11, 19], [8, 11, 19], [8, 11, 19, 37, 40]]
``````

print(New_i):

``````[17, 19, 30, 31, 34, 46]
``````

BTW I highly recommend not to use `I` and `i` for your variable names.