1

I have the following lists:

Factor1 = [1,2,3,4]

Factor2 = [5,6,7,8]

Factor3 = [9,10,11,12]

And the combination of the names of the lists:

Combination = [['Factor1','Factor2'], ['Factor3','Factor1']]

I would like to refer to the list Combination to combine the elements of the Factor* lists by zipping the list and the result as follows:

Combination1 = [(1,5),(2,6),(3,7),(4,8)]
Combination2 = [(9,1),(10,2),(11,3),(12,4)]

Note that I have simplified the problem because my Combination list has more than 1000 combinations while the current example has only 2. Hence, I wanted to iterate the process above.

The idea is to code as below:

Combination1 = list (zip(Factor1,Factor2))

But the problem is I am not sure how do you use the elements of the Combination list to as an input to the zip portion of the code.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Is it possible to put your `Factor` lists in a dictionary? Have the key as the list name and the value as the list itself. That way, you can access your list by name. Partially related: https://stackoverflow.com/q/47496415/2745495 – Gino Mempin Jul 16 '23 at 11:51
  • 2
    You can use `globals()['Factor*']` to get the corresponding list, where `*` is the number you want. Not a pretty solution, but it should work. – B Remmelzwaal Jul 16 '23 at 11:51
  • Lists don't *have names*. You have some strings. Then you have source code. Don't try to relate those two. If you want some string associated with some list, **make that association in your code**. You could use a `dict`, for example, to map those strings to lists – juanpa.arrivillaga Jul 16 '23 at 12:03

0 Answers0