-2

I want to find the new word in liste2.

liste1 = [['euro2016', '99'], ['portugal', '87'], ['ronaldo', '34']]
liste2 = [['euro2016', '90'], ['portugal', '75'], ['ronaldo', '44'], ['nani', '15']]


l1 = len(liste1)
l2 = len(liste2)

for x in range(0,l2):
    for y in range(0,l1):
        if liste2[x][0] not in liste1[y][0]:
            print liste2[x][0]

but my code is giving result like that :

euro2016

euro2016

portugal

portugal

ronaldo

ronaldo

nani

nani

nani

I guess I have to search liste1[all][0] but I dont know how to it.

syntonym
  • 7,134
  • 2
  • 32
  • 45

4 Answers4

1

check here

[i for i in liste2 if i[0] not in [j[0] for j in liste1]]
Matroskin
  • 429
  • 4
  • 5
1

I want to find the new word in liste2


You can use a list comprehension and apply a filter that takes only new items in liste2:

result = [i[0] for i in liste2 if i[0] not in (j[0] for j in liste1)]
#                              ^      filtering is done here       ^
print(result)
# ['nani']
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0
In [8]: dict1 = dict(liste1)

In [9]: dict2 = dict(liste2)

In [10]: print(set(dict2.keys()) - set(dict1.keys()))
set(['nani'])
Dean Fenster
  • 2,345
  • 1
  • 18
  • 27
0

You can create a set of the first element in each list item, and then take the difference. It will return the item in liste2 which is not in liste1:

l3 = set(map(lambda x:x[0],liste1))
l4 = set(map(lambda x: x[0],liste2))
print list(l4.difference(l3))[0]

Output:

nani
>>> 
Mukherjee
  • 486
  • 1
  • 3
  • 11