0

I currently have the following structure:

[('file_name', '', '', {'Key': [(msg_1), (msg_2)]})]

I want to insert the following tuple:

(time.struct_time(tm_year=2019, tm_mon=5, tm_mday=22, tm_hour=20, tm_min=19, tm_sec=54, tm_wday=2, tm_yday=142, tm_isdst=1))

Such that I end up with:

[('file_name', (time.struct_time(tm_year=2019, tm_mon=5, tm_mday=22, tm_hour=20, tm_min=19, tm_sec=54, tm_wday=2, tm_yday=142, tm_isdst=1),'', '', {'Key': [(msg_1), (msg_2)]}))]

Note that the closing bracket placement of the tuple of time.struct_time is required to be before the list ends (to enclose the remainder of the contents of msg1 and msg2).

I tried variations of using zip and converting to a list using insert with no luck.

excelsior
  • 95
  • 1
  • 9

1 Answers1

0

Tuples are immutable objects in python. It's not possible to change an existing tuple. Please take a look at this answer.

However, you can create another tuple:

a = [('file_name', '', '', {'Key': [(msg_1), (msg_2)]})]
b = (time.struct_time(tm_year=2019, tm_mon=5, tm_mday=22, tm_hour=20, tm_min=19, tm_sec=54, tm_wday=2, tm_yday=142, tm_isdst=1))
c = [a[0][0], b, *a[0][1:]]

Note that * before a[0][1:] unpack the selected slice of the tuple.

noidsirius
  • 409
  • 2
  • 12