64

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?

1
  • By the way, you might find the datetime type handy if you want to work with times rather than generic lists of strings. Commented Sep 16, 2010 at 12:55

6 Answers 6

130

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
Sign up to request clarification or add additional context in comments.

Comments

41
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.

3 Comments

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.
@DenisBarmenkov I don't see the problem. Can you point out the problem? Python repl: >>> set([1,2]) == set([1,1,2,2,2]) produces True
Sets contain unique numbers but lists have an order. [1,2] does not equal to [2,1]. Or we have to change topic question. Also you could take a look on likes count :/
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

11 Comments

dateArray = Thu Sep 16 13:14:15 CDT 2010
@Umesh - how is that an array?
sdateArray = Thu Sep 16 14:14:15 CDT 2010
Now both these dates were string I created array out of that but how do I compare them using for loop?
@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.
|
4

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.

Comments

2
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

1 Comment

While this code may answer the question, providing information on how and why it solves the problem improves its long-term value
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.