I am trying to do an inner join with 2 lists in python, without using pandas or numpy libraries.
The function combine has parameters: One: first list; Two: second list; left: indice of column in first list; right: indice of column in second list
I am trying to combine the 2 lists based on the indices of each list having elements that equal and then joining their elements together.
So far I have this code:
def combine(one, two, left, right):
combined = []
for x in one: #for elements in list one
for y in two: #for elements in list two
if x[left] == y[right]: #if both elements at each indice equal
combined = [x,y] #place x and y values into combined list
print(combined)
one = [['apple', 'fruit'],
['broccoli', 'vegetable']]
two = [['fruit', '1'],
['vegetable', '1']]
combine(one, two, 1, 0 )
For some reason i am getting an empty list: []
The desired output is:
[['apple', 'fruit', 'fruit', '1'],
['broccoli', 'vegetable', 'vegetable', '1']]
Any ideas/tips of how i can accomplish this?
I hope this code helps!
def combine(one, two, left, right):
combined = []
for x in one: #for elements in list one
for y in two: #for elements in list two
if x[left] == y[right]: #if both elements at each indice equal
combined.append(
x + y) #place x and y values into combined list
print(combined)
one = [['apple', 'fruit'], ['broccoli', 'vegetable']]
two = [['fruit', '1'], ['vegetable', '1']]
combine(one, two, 1, 0)
From my point of view, you should be calling the function like this:
combine(one, two, 1, 0 )
Since the position one[1] is the one that has the common value with two[0].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With