Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a tuple without using tuple function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading