Warm tip: This article is reproduced from serverfault.com, please click

Can't insert tuple into a list

发布于 2020-11-28 11:52:08

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?

Questioner
Shounak
Viewed
0
Maurice Meyer 2020-11-28 19:58:19

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)]