24

Possible Duplicate:
Python: How to find list intersection?

I have two lists of data in a .txt

data1 = "name1", "name2", "name3", "name4" etc.

data2 = "name3", "name6", "name10" etc.

I want to find out which names appears in both list How would I do it?

Community
  • 1
  • 1
ivanhoifung
  • 339
  • 2
  • 4
  • 9

4 Answers4

62

Use sets:

set(data1) & set(data2)

The & operator means "give me the intersection of these two sets"; alternatively you can use the .intersection method:

set(data1).intersection(data2)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    I find the .intersection more readable personally, but as you say they do the same thing. – TimothyAWiseman Jul 23 '12 at 15:02
  • 1
    @ivanhoifung If this solved your problem you should [accept this answer by clicking the checkmark](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) next to the answer. It'll reward both parties with some points, and mark this problem as solved. – Levon Jul 23 '12 at 15:18
  • This is a great answer and works for most cases (including the question posed above). However, this answer will not work if you are just trying to compare lists in any order and you want to account for duplicates as sets eliminate duplicates. – Peter Graham May 02 '17 at 19:53
  • 1
    @PeterGraham: in which case you'd use `Counter()` objects, which are multisets. – Martijn Pieters May 02 '17 at 21:31
  • Does this preserve the order (e.g two lists of sorted timestamps)? And are they computationally equivalent? – Maikefer Aug 20 '19 at 09:43
  • 1
    @Maikefer: sets do not retain ordering. In that case create a set only for `data2` and use a list comprehension that tests values against that set: `data2_set = set(data2)` and `[v for v in data1 if v in data2]`. – Martijn Pieters Aug 20 '19 at 09:45
  • @MartijnPieters Thanks! If I know that there are no duplicates in my list, then there is no need for the conversion into a set, right? – Maikefer Aug 20 '19 at 09:56
  • 1
    @Maikefer: you still want to create a set, because containment testing is way faster that way. I didn't consider duplicates in `data1`, you'd want to use `members = set(data1).intersection(data2)` and `[v for v in data1 if v in members]` to remove duplicates in that case. – Martijn Pieters Aug 20 '19 at 09:58
13
nf = [x for x in data1 if x in data2]
nf

would return the common item in both lists

Rakesh
  • 81,458
  • 17
  • 76
  • 113
1
>>> [ name for name in data1 if name in data2 ]
['name3']
bluish
  • 26,356
  • 27
  • 122
  • 180
Zulu
  • 8,765
  • 9
  • 49
  • 56
-3
For a in data1:
    for b in data2:
        if a==b:
        print(a)

That's one way to do it, not the best way though

Snaaa
  • 256
  • 1
  • 3
  • 14