How to combine two list of list in python without duplicates?



Hi friends,

I am new in python
I have list of list below:

[[1.0, 'Software Developer', 1329],
 [1.0, 'Software Developer', 2086],
 [0.9230769230769231, 'United States', 1329],
 [0.9230769230769231, 'United States', 2086]]

Now I want to combine these list of list from last element.
for example I want to combine list when last element is 1329 and need output below
[1.0, 'Software Developer', 0.9230769230769231, 'United States',1329]

And then Make into Dictionary below:
{'id': 1329,
'location': 'United States',
'location_score': 0.92,
'title': 'Software Developer',
'title_score': 1.0}

I tried and did google but I got just merging list only
How can I combine list of list with common element?

Thanks in advance



I could think of two ways for doing this:

  1. Use .extend and del

Example: I have two lists , list1 = [1, 2, 3, 7] and list2 = [4, 5, 6, 7]
I can use the following commands:

del list1.[3]

  1. Use if-else and for loop over the list.

If the last elements of two lists are same, remove the last element of list1, and using for loop, add all the elements from list2 to list1.


Thanks for kind answer. But its list of list so I am getting trouble in it


You can extract individual list from the list of lists,

list= [[1,2,3,7],[4,5,6,7]]


or simply use for loop to extract.


@AishwaryaSingh Thanks for reply
L = [[1.0, 'Software Developer', 1329],
[1.0, 'Software Developer', 2086],
[0.9230769230769231, 'United States', 1329],
[0.9230769230769231, 'United States', 2086]]

for i in range(0,len(L)):
    for j in range(0,len(L)-1):  

from above code I got elements of list of list but Now I am not getting how to combine then with common value



Hi Tried and get answer but still some difficulties:

final =[]
for i in range(0,len(competency_sort)-2):
    if competency_sort[i][2] == competency_sort[i][2]:

After Run this code I got desire output but my concern now about how to put dynamically value in i+2 .
I put 2 because after 2 list last element repeat. But I am not getting how to make dynamically so if last element repeat after 3 list or 4 list it will give me desire output.


Hi @premsheth

Presently, the code checks the last value for every alternate list (within L). You can instead check and compare the last element of every list. Although the number of iterations will increase, but the code will run for all possible cases.


@AishwaryaSingh Thanks for you kind reply but I got answer and it is below:

from collections import defaultdict

# initialize defaultdict of dicts
final = defaultdict(dict)

# calculate half length of list
n = int(len(competency_sort)/2)

# iterate first part of list
for title_score, title, ID in competency_sort[:n]:
    final[ID]['title_score'] = title_score
    final[ID]['title'] = title

# iterate second part of list
for Location_score, Location, ID in competency_sort[n: len(competency_sort)]:
    final[ID]['Location_score'] = Location_score
    final[ID]['Location'] = Location

final_model = [{**{'ID': k}, **v} for k, v in final.items()] 

It resolved. Thank you so much for your replies


l=[[1.0, ‘Software Developer’, 1329],
[1.0, ‘Software Developer’, 2086],
[0.9230769230769231, ‘United States’, 1329],
[0.9230769230769231, ‘United States’, 2086]]

for x in l:
id = x[-1]
if id not in d:
d[id] = x [:-1]
d[id] += x[:-1]

for k,v in d.items():
d[k]={‘location’: location,
‘location_score’: location_score,

{1329: {‘location’: ‘United States’, ‘location_score’: 0.9230769230769231, ‘title’: ‘Software Developer’, ‘title_score’: 1.0}, 2086: {‘location’: ‘United States’, ‘location_score’: 0.9230769230769231, ‘title’: ‘Software Developer’, ‘title_score’: 1.0}


Here is my solution

l = [[1.0, ‘Software Developer’, 1329],
[1.0, ‘Software Developer’, 2086],
[0.9230769230769231, ‘United States’, 1329],
[0.9230769230769231, ‘United States’, 2086]]

Create a dictionary from the list


dictry = dict()
for x in l:
id = x[-1]
if id not in dictry:
dictry[id] = x [:-1]
dictry[id] += x[:-1]

for k,v in dictry.items():
title_score = dictry[k][0]
title = dictry[k][1]
locatn_score = dictry[k][2]
locatn = dictry[k][3]
dictry[k]= {‘location’: locatn,
‘location_score’: locatn_score,

final_dict = [{**{‘ID’: k}, **v} for k, v in dictry.items()]
[{‘ID’: 1329, ‘location_score’: 0.9230769230769231, ‘title’: ‘Software Developer’, ‘title_score’: 1.0, ‘location’: ‘United States’},
{‘ID’: 2086, ‘location_score’: 0.9230769230769231, ‘title’: ‘Software Developer’, ‘title_score’: 1.0, ‘location’: ‘United States’}]


Here is my solution:

first_list=[[1.0, ‘Software Developer’, 1329],
[1.0, ‘Software Developer’, 2086],
[0.9230769230769231, ‘United States’, 1329],
[0.9230769230769231, ‘United States’, 2086]]
for i in first_list:
if i[-1]==1329: #checking if last element is 1329
second_list.append(1329) #this line is out of loop
#as you know the last element was 1329 so add it later.

output of second list will be:

[1.0, ‘Software Developer’, 0.9230769230769231, ‘United States’,1329]

Reply if you find it helpful or any difficulty in code :slight_smile:


@ujjwal18 Thank you so much for your kind reply. It’s Half solution but It’s good.