Can't insert tuple into a list

Viewed 101

I am trying to insert a date object in a list. But, on execution, the code raises a TypeError. Here's the code:

values.append(datetime.date(tuple(checkIn)))
values.append(datetime.date(tuple(checkOut)))

For viewing pleasure, I provided only the lines that raises the error.

Here,

  • values is a list
  • checkIn and checkOut are lists. eg- checkIn = [2020, 11, 28]

This is the error message:

File "d:/coding/python/hms.py", line 195, in booking
    values.append(dt.date(tuple(checkIn)))
TypeError: an integer is required (got type tuple)

So, why can't I insert tuple into a list?

2 Answers

You need to unpack checkIn and checkOut in order to pass the values as arguments:

import datetime as dt

values = []
checkIn = [2020, 11, 28]

values.append(dt.date(*checkIn))
print(values)

Out:

[datetime.date(2020, 11, 28)]

Try:

checkin=[yy,mm,dd]
values.append(datetime.date(checkin))

because the type error.

Related