Home > Mobile >  Merge every x element in multiple lists to return new list
Merge every x element in multiple lists to return new list

Time:12-10

I'm writing a script that scrapes all of the data from my works ticketing site and the end goal is to have it send a text when a new ticket enters the bucket with all of the important info of the ticket.

Python 3.10

So far, it pulls from a scattered list and combines all of the elements into an appropriate group ie. ticket numbers,titles and priorities.

tn = rawTickets[0::14]
title = rawTickets[5::14]
priority = rawTickets[9::14]

With this I can say

num = x
wholeticket = tn[num], title[num], priority[num],
print(wholeticket)

and get x ticket in the list

# Results: "tn0, title0, priority0"

I want it to print all of the available tickets in the list based on a range

totaltickets = 0
for lines in rawTickets:
    if lines == '':
        totaltickets  = 1
numrange = range(totaltickets)

so lets say there are only 3 tickets in the queue, I want it to print

tn0, title0, priority0,
tn1, title1, priority1,
tn2, title2, priority2,

But I want to avoid doing this;

ticket1 = tn[0], title[0], priority[0],
ticket2 = tn[1], title[1], priority[1],
ticket3 = tn[2], title[2], priority[2],

flowchart to help explain

CodePudding user response:

You could use zip:

tickets = list(zip(rawTickets[0::14], rawTickets[5::14], rawTickets[9::14]))

This will give you a list of 3-tuples.

CodePudding user response:

You could do something like that:

l1 = [*range(0,5)]
l2 = [*range(5,10)]
l3 = [*range(10,15)]

all_lst = [(l1[i], l2[i], l3[i]) for i in range(len(l1))]

Or you could use zip as trincot offered. Note that on large scales, zip is much faster.

  • Related