0

I have a tuple with the following structure

#list of tuples
[('Persons', datetime.datetime(2020, 5, 11, 11, 31, 0, 160000), datetime.datetime(2020, 5, 11, 12, 8, 25, 320000))]
#[0] index position
('Persons', datetime.datetime(2020, 5, 11, 11, 31, 0, 160000), datetime.datetime(2020, 5, 11, 12, 8, 25, 320000))

Now how to identify whether datetime.datetime(2020, 5, 11, 11, 31, 0, 160000) and datetime.datetime(2020, 5, 11, 12, 8, 25, 320000) are identical or not (equality test)?

I have answered the question

Regards

CK5
  • 1,055
  • 3
  • 16
  • 29
  • Does this answer your question? [Check if all elements in a list are identical](https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical) – Michael Bianconi May 11 '20 at 12:51

2 Answers2

2

Datetime supports equality tests.

a = [('Persons', datetime.datetime(2020, 5, 11, 11, 31, 0, 160000), datetime.datetime(2020, 5, 11, 12, 8, 25, 320000))]
print(a[0][0] == a[0][1])  # False
  • Hi @Will Derriman ...It is showing false for even equal elements. For example, it is showing false for `[('brand_approvals', datetime.datetime(2020, 5, 6, 18, 5, 9, 600000), datetime.datetime(2020, 5, 6, 18, 5, 9, 600000))]`. Thank you for answering :) – CK5 May 11 '20 at 13:03
  • Hi @KrisT, very interesting. `dt1 = datetime.datetime(2020, 5, 11, 11, 31, 0, 160000) dt3 = datetime.datetime(2020, 5, 11, 11, 31, 0, 160000) dt2 = datetime.datetime(2020, 5, 11, 12, 8, 25, 320000) print(dt1 == dt2) # False print(dt1 == dt3) # True` – Will Derriman May 11 '20 at 13:08
0

The following code worked for me to identify identical tuple index items.

       #list of tuples
        row = [('Persons', datetime.datetime(2020, 5, 11, 11, 31, 0, 160000), datetime.datetime(2020, 5, 11, 12, 8, 25, 320000))]   


        #converting list to a tuple
        listT = row[0]
        listT = ('Persons', datetime.datetime(2020, 5, 11, 11, 31, 0, 160000), datetime.datetime(2020, 5, 11, 12, 8, 25, 320000))

        #comparing tuple index positions
        if listT[1] == listT[2]:
            print('both are equal')
        else:
            print('they are not equal')
CK5
  • 1,055
  • 3
  • 16
  • 29