55

How to compare two lists in python?

date = "Thu Sep 16 13:14:15 CDT 2010" 
sdate = "Thu Sep 16 14:14:15 CDT 2010" 
dateArr = [] dateArr = date.split() 
sdateArr = [] sdateArr = sdate.split() 

Now I want to compare these two lists. I guess split returns a list. We can do simple comparision in Java like dateArr[i] == sdateArr[i], but how can we do it in Python?

jinawee
  • 479
  • 5
  • 15
Umesh K
  • 12,846
  • 22
  • 85
  • 126
  • By the way, you might find the [datetime](http://docs.python.org/library/datetime.html#datetime-objects) type handy if you want to work with times rather than generic lists of strings. – Muhammad Alkarouri Sep 16 '10 at 12:55

6 Answers6

111

You could always do just:

a=[1,2,3]
b=['a','b']
c=[1,2,3,4]
d=[1,2,3]

a==b    #returns False
a==c    #returns False
a==d    #returns True
skajfes
  • 7,865
  • 2
  • 22
  • 23
35
a = ['a1','b2','c3']
b = ['a1','b2','c3']
c = ['b2','a1','c3']

# if you care about order
a == b # True
a == c # False

# if you don't care about order AND duplicates
set(a) == set(b) # True
set(a) == set(c) # True

By casting a, b and c as a set, you remove duplicates and order doesn't count. Comparing sets is also much faster and more efficient than comparing lists.

Vito Gentile
  • 12,019
  • 9
  • 58
  • 89
PyRsquared
  • 6,033
  • 9
  • 44
  • 77
  • 6
    Its incorrect to turn arrays into sets - it will throw duplicated items, not only remove order. Try to compare arrays [1,2] and [1, 1, 2, 2, 2] with sets. – Denis Barmenkov Apr 19 '19 at 23:04
5

If you mean lists, try ==:

l1 = [1,2,3]
l2 = [1,2,3,4]

l1 == l2 # False

If you mean array:

l1 = array('l', [1, 2, 3])
l2 = array('d', [1.0, 2.0, 3.0])
l1 == l2 # True
l2 = array('d', [1.0, 2.0, 3.0, 4.0])
l1 == l2 # False

If you want to compare strings (per your comment):

date_string  = u'Thu Sep 16 13:14:15 CDT 2010'
date_string2 = u'Thu Sep 16 14:14:15 CDT 2010'
date_string == date_string2 # False
Dominic Rodger
  • 94,357
  • 33
  • 195
  • 210
  • dateArray = Thu Sep 16 13:14:15 CDT 2010 – Umesh K Sep 16 '10 at 11:55
  • @Umesh - how is that an array? – Dominic Rodger Sep 16 '10 at 11:56
  • sdateArray = Thu Sep 16 14:14:15 CDT 2010 – Umesh K Sep 16 '10 at 11:56
  • Now both these dates were string I created array out of that but how do I compare them using for loop? – Umesh K Sep 16 '10 at 11:57
  • @Umesh - post the code where you created the two strings, otherwise all of us are just guessing at what you're trying to do. – Dominic Rodger Sep 16 '10 at 11:58
  • @Umesh: this information belongs in the body of the question, not comments. – SilentGhost Sep 16 '10 at 11:58
  • I know it looks like string you can understand them as arrays. – Umesh K Sep 16 '10 at 11:58
  • I am sorry for the trouble actually I am using StackOverflow for the first time so not familiar with UI. – Umesh K Sep 16 '10 at 12:00
  • date= "Thu Sep 16 13:14:15 CDT 2010" – Umesh K Sep 16 '10 at 12:03
  • date = "Thu Sep 16 13:14:15 CDT 2010" sdate = "Thu Sep 16 14:14:15 CDT 2010" dateArr = [] dateArr = date.split() sdateArr = [] sdateArr = sdate.split() Now I want to compare these two array I guess split returns array. We can do simple comparision in Jav like dateArr[i] == sdateArr[i] but how can we do it in Java? Sorry for trouble Please help – Umesh K Sep 16 '10 at 12:07
  • 2
    @Umesh Kacha: Scroll up and look at your question. Below it there are several buttons. The second from the left says `edit`. This can be used to edit your question, for instance if you want to add additional information. – Björn Pollex Sep 16 '10 at 12:09
3

Given the code you provided in comments, I assume you want to do this:

>>> dateList = "Thu Sep 16 13:14:15 CDT 2010".split()
>>> sdateList = "Thu Sep 16 14:14:15 CDT 2010".split()
>>> dateList == sdataList
false

The split-method of the string returns a list. A list in Python is very different from an array. == in this case does an element-wise comparison of the two lists and returns if all their elements are equal and the number and order of the elements is the same. Read the documentation.

Björn Pollex
  • 72,744
  • 28
  • 189
  • 274
1
for i in arr1:
    if i in arr2:
        return 1
    return  0
arr1=[1,2,5]
arr2=[2,4,15]
q=checkarrayequalornot(arr1,arr2)
print(q)
>>0
ravi tanwar
  • 536
  • 5
  • 16
  • While this code may answer the question, providing information on how and why it solves the problem improves its long-term value – L_J Jul 21 '18 at 08:51
0

From your post I gather that you want to compare dates, not arrays. If this is the case, then use the appropriate object: a datetime object.

Please check the documentation for the datetime module. Dates are a tough cookie. Use reliable algorithms.

Escualo
  • 38,824
  • 20
  • 81
  • 128