16

So to give a rough example without any code written for it yet, I'm curious on how I would be able to figure out what both lists have in common.

Example:

listA = ['a', 'b', 'c']
listB = ['a', 'h', 'c']

I'd like to be able to return:

['a', 'c']

How so?

Possibly with variable strings like:

john = 'I love yellow and green'
mary = 'I love yellow and red'

And return:

'I love yellow and'
Matthew
  • 837
  • 3
  • 18
  • 33

5 Answers5

34

Use set intersection for this:

list(set(listA) & set(listB))

gives:

['a', 'c']

Note that since we are dealing with sets this may not preserve order:

' '.join(list(set(john.split()) & set(mary.split())))
'I and love yellow'

using join() to convert the resulting list into a string.

--

For your example/comment below, this will preserve order (inspired by comment from @DSM)

' '.join([j for j, m in zip(john.split(), mary.split()) if j==m])
'I love yellow and'

For a case where the list aren't the same length, with the result as specified in the comment below:

aa = ['a', 'b', 'c']
bb = ['c', 'b', 'd', 'a']

[a for a, b in zip(aa, bb) if a==b]
['b']
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    Is there anyway to preserve order? – Matthew Jul 28 '12 at 02:37
  • 2
    @Matthew: what would you like this method to return in case of `['a', 'b', 'c']` and `['c', 'b', 'd', 'a']`? – liori Jul 28 '12 at 02:38
  • 1
    I'd like to see it return `['b']`. – Matthew Jul 28 '12 at 02:40
  • `' '.join([j for j, m in zip(john.split(), mary.split()) if j==m])` is exactly what I'm looking for! Now to look into seeing how you did it to learn myself! Thanks so much! How would I go about doing it to check a total of say.. 5 strings at the same time? – Matthew Jul 28 '12 at 02:43
  • @Matthew You could extend this example to more lists, zip can handle more than two lists, e.g., `for i,j,k in zip(aa, bb, cc): print i, j, k` .. if your problem ends up being more complex other solutions might exist (but that should be a different question/post) – Levon Jul 28 '12 at 02:53
  • I'm not following, how can I incorporate that into `' '.join([j for j, m in zip(john.split(), mary.split()) if j==m])`? – Matthew Jul 28 '12 at 03:00
  • @Matthew The solution I provided had 2 lists (based on your post and title of the post), so I use `j`, `m`, and compare `j==m` .. if you want to extend this to 3 lists you'll have to use more variables, more comparisons, and you'll have to provide a third list to `zip`. It's a direct extension of this solution. If you aren't comfortable with zip etc, you may want to explore the command (and the solution for 2 lists provided above) interactively step by step in the Python shell and get a feel for how this works. – Levon Jul 28 '12 at 03:07
2

If the two lists are the same length, you can do a side-by-side iteration, like so:

list_common = []
for a, b in zip(list_a, list_b):
    if a == b:
        list_common.append(a)
algorowara
  • 1,700
  • 1
  • 15
  • 15
2

If order doesn't matter, but you want to print out a nice diff:


def diff(a, b):
  if len(set(a) - set(b)) > 0:
    print(f"Items removed: {set(a) - set(b)}")
  if len(set(b) - set(a)) > 0:
    print(f"Items added: {set(b) - set(a)}")
  if set(a) == set(b):
    print(f"They are the same")

diff([1,2,3,4], [1,2,3])
# Items removed: {4}

diff([3,4], [1,2,3])
# Items removed: {4}
# Items added: {1, 2}

diff([], [1,2,3])
# Items added: {1, 2, 3}

diff([1,2,3], [1])
# Items removed: {2, 3}

listA = ['a', 'b', 'c']
listB = ['a', 'h', 'c']
diff(listA, listB)
# Items removed: {'b'}
# Items added: {'h'}

john = 'I love yellow and green'
mary = 'I love yellow and red'
diff(john, mary)
# Items removed: {'g'}

ubershmekel
  • 11,864
  • 10
  • 72
  • 89
1

Intersect them as sets:

set(listA) & set(listB)
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Could you give me a working example on how I could about using it with: john = 'I love yellow and green' mary = 'I love yellow and red' And return: 'I love yellow and' – Matthew Jul 28 '12 at 02:25
  • 1
    `' '.join(set(john.split(' ')) & set(mary.split(' ')))` – Ry- Jul 28 '12 at 02:27
1

i think this is what u want ask me anything about it i will try to answer it

    listA = ['a', 'b', 'c']
listB = ['a', 'h', 'c']
new1=[]
for a in listA:
    if a in listB:
        new1.append(a)

john = 'I love yellow and green'
mary = 'I love yellow and red'
j=john.split()
m=mary.split()
new2 = []
for a in j:
    if a in m:
        new2.append(a)
x = " ".join(new2)
print(x)