Sandy
Sandy

Reputation: 263

Iterate over multiple list and creating list with outcome of result in Python

I am new in Python, before I worked on Perl and C.

I am running into simple problem, which can be easily nail down by the Python experts.

I have lists , the value on the list can be random.

list_a = [True, False]
list_b = [False, True]
list_c = [True]

I need to iterate over multiple list. I have researched and got the following working.

 output_list = []

 for i,j,k in map(None, list_a, list_b, list_c):

      output_list.append([i,j,k])

But the challenge I am facing challenge is After the output it is generating output something like this : [(True,False,True), (False,True, None)]

output_list[0] = [True,False,True] output_list[1] = [False,True, None]

Expectation is should return give a clue about lists.

output_list[0] = [True_list_a,False_list_b,True_list_c] output_list[1] = [False_list_a,True_list_b, None_list_c]

I have no clue of output I got output_list is of list_a or list_b or list_c.

How to create the output_list with corresponding list_a, list_b, list_c element, so that I can send to the function.

Is there any way to find it out? Thanks in advance.

Upvotes: 0

Views: 64

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92874

Just a guess. Perhaps, you wanted something like this(in regard with your comment " ... output_list should have a clue about elements of which list it is"):

import itertools

list_a = [True, False]
list_b = [False, True]
list_c = [True]

output_list = [{'list_a':t[0], 'list_b':t[1], 'list_c':t[2]}
               for t in itertools.zip_longest(list_a, list_b, list_c)]

print(output_list)

The output:

[{'list_a': True, 'list_b': False, 'list_c': True}, {'list_a': False, 'list_b': True, 'list_c': None}]

Upvotes: 1

Andrey Belykh
Andrey Belykh

Reputation: 2654

I am guessing you want something like that:

import itertools
output_list = list(itertools.chain(list_a, list_b, list_c))

Upvotes: 0

Related Questions