2

I am new to Python as well new on Stackoverflow , can anyone tell me efficient(pythonic) way to compare more than 2 Lists ? I want to enlist all the elements of all 3 Lists and display in such fashion that user will be able to know that which element is present in all 3 List OR element present in List 1 but not in List 2 OR which elements are duplicates. I have done comparison using nested loops.

List1 = [10,10,11,12,15,16,18,19]  
List2 = [10,11,13,15,16,19,20]  
List3 = [10,11,11,12,15,19,21,23] 

# Checking whether List 1 value present in List2 and List 3
for l1 in List1:  
    if l1 in List2: 
        List2.remove(l1)  
        if l1 in List3: 
            List3.remove(l1)  
            print(l1," ",l1," ",l1)  
        else:  
            print(l1," ",l1," ","NA")  
    else:
        if l1 in List3:   
            List3.remove(l1)
            print(l1," ","NA"," ",l1)
        else:
            print(l1," ","NA"," ","NA")

# Checking whether List 2 value present in List3
for l2 in List2:
    if l2 in List3:
        List3.remove(l2)
        print("NA"," ",l2," ",l2)
    else:
        print("NA"," ",l2," ","NA")

# Checking for values present only in List 3

for l3 in List3:
    print("NA","NA",l3)

--- Output---
List1 List2 List3
10   10   10
10   NA   NA
11   11   11
12   NA   12
15   15   15
16   16   NA
18   NA   NA
19   19   19
NA   13   NA
NA   20   NA
NA NA 11
NA NA 21
NA NA 23
NA   20   NA
NA NA 11
NA NA 21
NA NA 23

Is there any better way to compare the Lists ?

Aniketan
  • 55
  • 2
  • 9
  • 1
    What do you exactly want to do? If this is about checking in what lists values can be found, you can use a dictionary for instance. – zaphodef Apr 01 '16 at 12:34
  • 7
    I guess a more pythonic way would be sets. Then just use intersect and difference – Schore Apr 01 '16 at 12:36
  • 2
    What is the goal you are trying to achieve in this comparison? What is the expected output with your comparison? – idjaw Apr 01 '16 at 12:44
  • BTW, it's generally not safe to remove items from a list that you're iterating over in the forward direction. It's a bit like sawing off a tree branch that you're sitting on. One way around that is to create a new list, copying the items you want to keep. But as others have said, you can probably do what you want more efficiently using sets. However, you _do_ need to explain more clearly what you're actually trying to achieve. What result do you want from that sample data? – PM 2Ring Apr 01 '16 at 12:53
  • Sorry! if my question is not clear to you. I just want to compare 3 List data and write it in file, I want to enlist all the elements of 3 Lists and display in such fashion that user will be able to know that which element is present in all 3 List OR element present in List 1 but not in List 2 OR which elements are duplicates. @Schore can you please explain , how it can be achieved by using sets ? – Aniketan Apr 02 '16 at 09:00
  • @PM2Ring Can you please explain me with any example how removing items form List that we are iterating over in the forward direction is not safe ? – Aniketan Apr 05 '16 at 13:35
  • @Aniketan: Please see [Removing from a list while iterating over it](http://stackoverflow.com/q/6500888/4014959). For further info, see [this SO Python page](http://sopython.com/canon/95/removing-items-from-a-list-while-iterating-over-the-list). – PM 2Ring Apr 05 '16 at 13:42

3 Answers3

6

If you want the intersection of all the lists, make one a set and pass the others to .intersection:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

print(set(List1).intersection(List2, List3))

Which gives you:

set([19, 10, 11, 15])

To get the unique element from List3:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

inter = set(List1).intersection(List2, List3)

diff3 = set(List3).difference(inter)

print(diff3)
set([12, 21, 23])

If you want to only keep the elements in each list that are not in the intersection:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

inter = set(List1).intersection(List2, List3)

List1[:] = [ele for ele in List1 if ele not in inter]
List2[:] = [ele for ele in List2 if ele not in inter]
List3[:] = [ele for ele in List3 if ele not in inter]

print(List1)
print(List2)
print(List3)

Which will give you:

[12, 16, 18]
[13, 16, 20]
[12, 21, 23]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
5
import pandas as pd
d = {'List1' : pd.Series(List1),'List2' : pd.Series(List2),'List3': pd.Series(List3)}

df = pd.DataFrame(d)

print(df)

***OUTPUT:***
      List1  List2  List3
      10      NaN     NaN
      11      11     11
      12      NaN     12
      15      15     15
      16      16     NaN
      18      NaN    NaN
      19      19     19
      NaN     13     NaN

Using Pandas you can compare the multiple list, while the empty coloumn would be automatically filled as NaN.

Pandas is a Data visualization library of python

Install pandas by : pip install pandas

Edited :

Gist Link: https://gist.github.com/gr8Adakron/b51cc060b5e6dcc030261586f7237232

The Gr8 Adakron
  • 1,200
  • 1
  • 12
  • 15
3

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

>>> set(List1).intersection(set(List2))
set([16, 19, 10, 11, 15])
>>> set(List1).intersection(set(List3))
set([19, 10, 11, 12, 15])
>>> set(List2).intersection(set(List3))
set([19, 10, 11, 15])
>>> 

intersection produces the numbers in both sets. Set contains the different values in list without duplicates

Your question is unclear to me as to whether you want to consider duplicates, and if so how.

joel goldstick
  • 4,393
  • 6
  • 30
  • 46