0

This question, though specific in nature, can be abstracted to a general question about Python.

Imagine there is a function that takes in a few parameters. This list of parameters is of variable length; however, the types of the given parameters are not variable. The parameters come in groups of three, and the first and second are lists of floats and the third is a string. Then it repeats. So, in a way, the parameters are not of variable length, it's restricted to multiples of 3.

The types can be anything, those are just the ones specific to my problem. I have a matplotlib plot function that looks like this in the docs:

a.plot(x1, y1, 'g^', x2, y2, 'g-',...)

As you can see, number of groups of three can be however long you want.

I have a list of lists that contains all of my x values (x1, x2, x3, ...) and a list of lists that contain all of my y values (y1, y2, y3,...). Not knowing how long those lists are (they are always equal in length to each other, though), and assuming I can have some dictionary that maps certain indexes to certain strings (for the string parameter), how can I pass indexes from variables length list of lists to this function.

Ideally, I guess it would look something like this is pseudo-code:

for element in list_of_lists:
    myFunction(lists_of_lists[element])

Except that this code would just execute the myFunction for all the elements in the list_of_lists. Instead, I want one long list of parameters and only execute the function once. I also feel like this problem is interested for Python as a whole, not just my specific issue. Thanks in advance!

2
  • 2
    I think the simplest way to do this would be to use a list of tuples: [(x1, y1, 'g^'), (x2, y2, 'g-')], etc. If the arguments only make sense when all three are provided together, use a data structure that keeps them together. To actually create such tuples from your list of lists, the zip builtin is probably the starting point, although your dictionary of string indices will complicate things. Commented Nov 7, 2013 at 19:44
  • What I mean is, given the tuples, how do i turn [(x1, y1, 'g^'), (x2, y2, 'g-')] into plot(x1, y1, 'g^',x2, y2, 'g-') Commented Nov 7, 2013 at 19:56

1 Answer 1

0

Given n lists of equal length (for example):

a = [x1, x2, x3]
b = [y1, y2, y3]
c = ['g^', 'g-', 'g+']

zip will take one element from each list and put them into a tuple in the order they were passed in.

From our example, zip(a, b, c) returns:

[(x1, y1, 'g^'), (x2, y2, 'g-'), (x3, y3, 'g+')]

Now to pass this to plot:

list_of_tuples = zip(a, b, c)
denormalized = [x for tup in list_of_tuples for x in tup]
plot(*denormalized)

[x for tup in list_of_tuples for x in tup] is a list comprehension that will execute for x in tup for each tup in list_of_tuples and append each x in order to the final list. In other words, it flattens the list of tuples into a list.

In our example, denormalized becomes

[x1, y2, 'g^', x2, y2, 'g-', x3, y3, 'g+']

It is functionally equivalent to:

denormalized = []
for tup in list_of_tuples:
    for x in tup:
        denormalized.append(x)

The unpack operator (*) when applied to a list tells python to call that function with each element in the list acting as if it were passed in as a positional argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks for deciphering my poorly worded question and coming up with awesome code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.