I have to create a list of tuples without using the tuple() function. The input is a list of lists where each list contains a profile ID and the calendar date on which the user went of a date. The output is a list of tuples showing the profile ID and the number of dates the user went on.
The final output should be a list of tuples, but my code outputs each element individually.
Below is the code I wrote to try to convert the list elements to tuples.
for stuff in datingTrack:
for smallerStuff in stuff:
tuplesList = (smallerStuff)
datingTrack2.append(tuplesList)
input: [["B111", "10/2/2022"], ["B222", "9/25/2022"], ["B333", "8/1/2022"], ["B222", "9/2/2022"]]
my output: ['B111', 1, 'B222', 2, 'B333', 1]
Expected output: [('B111', 1,) ('B222', 2), ('B333', 1)]
>Solution :
If you’re forced to not use the tuple construtor, this will do it
my_input = [["B111", "10/2/2022"], ["B222", "9/25/2022"], ["B333", "8/1/2022"], ["B222", "9/2/2022"]]
my_output = [(*item,) for item in my_input]
# [('B111', '10/2/2022'), ('B222', '9/25/2022'), ('B333', '8/1/2022'), ('B222', '9/2/2022')]