0

I am trying to construct a list from the printed output of a for-loop.

In the example below based on some data I do a for-loop for some calculations (all of them needed) to create a new variable, which is printed after each iteration. What I need is to create a list from the printed values of this new variable.

The data and the for-loop code:

from collections import defaultdict
from itertools import permutations

#data
list_type = [['A', 'B'], ['B'], ['A', 'B', 'C', 'D']]
list_xx = [[1, 5], [3], [2, 7, 3, 1]]

#for-loop
wd = defaultdict(float)
for i, x in zip(list_type, list_xx):
    # staff 1
    if len(i) == 1:
        print('aaa')
        continue
    # staff 2
    pairs = list(permutations(i, 2))
    dgen = (value[0] - value[1] for value in permutations(x, 2))
    # staff 3
    for team, result in zip(i, x):
        win_comp_past_difs = sum(wd[key] for key in pairs if key[0] == team)
        print(win_comp_past_difs)
    # staff 4
    for pair, diff in zip(pairs, dgen):
        wd[pair] += diff

# which prints the following result:
0.0
0.0
aaa
-4.0
4.0
0.0
0.0

What I need is to construct a list from the printed values of this for-loop, which should look like this:

[0, 0, 'aaa', -4, 4, 0, 0]

I would really value an efficient way of doing this in Python.

Mario Arend
  • 459
  • 4
  • 16
  • 2
    declare a blank list before the iteration starts and then `list.append` at every iteration where you print the result? or are you looking for a faster way of the iteration? – anky Feb 16 '20 at 15:12
  • I am just trying to create a list. – Mario Arend Feb 16 '20 at 15:14
  • 1
    does this help? : [Python- How to generate new list from variable in a loop?](https://stackoverflow.com/questions/16888880/python-how-to-generate-new-list-from-variable-in-a-loop) – anky Feb 16 '20 at 15:16

0 Answers0