Home > Blockchain >  Interacting through my list and making news lists based on indexes
Interacting through my list and making news lists based on indexes

Time:01-07

So i am doing the following interation.

for i in cleaned_rows:
    for j in i:
        print(j)

Which results in the following

2021-12-28T20:44:35.262Z
2021-12-28
[email protected]
[email protected]
onboarding return-start view
Exibicao
{city:null
2021-12-28T20:44:35.733Z
2021-12-28
[email protected]
[email protected]
onboarding segment-confirmation origin
onboarding return-start
{city:null
2021-12-28T20:44:35.412Z
2021-12-28
[email protected]
[email protected]
onboarding return-start origin
product-selection combos-packages-redirection
{city:null

For those curious this is what i[0] looks like:

    [2021-12-28T20:44:35.262Z
    2021-12-28
    [email protected]
    [email protected]
    onboarding return-start view
    Exibicao
    {city:null]

As you can see after the seventh value it repeats itself, basically another list is interated through. The thing is i want to do is pick the first index from my first list, and associate it to the following first index from the others lists and put into a list, ignoring the suceding indexes. The same goes to the second index from my first list, associate to the following second indexes appending it a list, and it goes unto the seventh index or 'sixth'. Hope you guys help me.

Desired outcome:

List 1 : [2021-12-28T20:44:35.262Z,2021-12-28T20:44:35.733Z,2021-12-28T20:44:35.412Z]
List 2: [2021-12-28,2021-12-28,2021-12-28]

And it goes

CodePudding user response:

Given a list of list e.g. data = [[a1, b1, c1], [a2, b2, c2], [a3, b3, c3], ...] you can obtain the output separated = [[a1, a2, a3, ...], [b1, b2, b3, ...], [c1, c2, c3, ...] by list(map(list, zip(*data)))

The * operator provides an iterator over data which provides each element of data, i.e., each of the inner lists, separately to the zip function.

The zip function takes multiple iterable objects and provides tuple of their elements in order, i.e, first tuple is the first element of each list.

We turn the tuples into lists by map and apply list function to each.

We turn the resulting map output into a list.

Note that for this to work you require all inner lists to have the same number of elements as zip will not return an nth tuple if any of the inner lists do not have an nth element.

  •  Tags:  
  • Related