Can multiple lists be assigned values in just one list comprehension?

With regards to some of the tasks in the [String Methods -Review],is it possible to assign values to multiple lists using one line of code or using a list comprehension?

so if this is acceptable:
var1, var2, var3 = ("shoe",3,"apple")

…can this work too?
list1, list2, list3 = [item[0], item [1], item[2] for item in my_other_list]

I tried it in the review exercise but got this error:

File "script.py", line 9
    titles, poets, dates = [item[0], item [1], item[2] for item in highlighted_poems_details]
                                                         ^
SyntaxError: invalid syntax

…then thought " oh! it needs to be surrounded by parentheses!" and tried this:


highlighted_poems_details = [poems.split(':') for poems in highlighted_poems_stripped]
titles, poets, dates = [(item[0], item [1], item[2]) for item in highlighted_poems_details]

but got this error:

 Traceback (most recent call last):
   File "script.py", line 9, in <module>
     titles, poets, dates = [(item[0], item [1], item[2]) for item in highlighted_poems_details]
 ValueError: too many values to unpack (expected 3)

highlighted_poems_details is a list that contains lists of details. If each item is a list and slicing it at the designated index gives the value for each list and works when you parse it on separate lines…

titles = [item[0]for item in highlighted_poems_details]
poets = [item[1]for item in highlighted_poems_details]
dates = [item[2]for item in highlighted_poems_details]

…why does it generate the ValueError of “too many values to unpack” when you try to do it in a list comprehension?

Am I completely out to lunch following this logic, or have I yet to learn the techniques necessary to make it work ?

Hi @blogrunner22065 ,
If we want to assign all 3 lists to the same list value then we can go with this(?) -

list1 = list2 = list3 = my_other_list,
The main problem is list aliasing as it’s a reference type. All these lists will have same reference and thus same value and changing one would change all of them.

Also your code [item[0], item [1], item[2] for item in my_other_list] , basically what you’re trying to do here is … loop over your list and asking python to give that’s items first index value, second index value, third index value (Which is a wrong syntax), also even when you use this syntax …
[item[0] for item in my_other_list] , It would only work if your list items are iterable and would fail for number or boolean types…

3 Likes

This is creating a list of tuples.

a, b, c = the_list

Python can only unpack one tuple into the awaiting variables, not the entire list.

a, b, c = this_list[0]

will unpack the first tuple.

1 Like

Because the parser treats each sublist (list of three items) of the list highlighted_poems_stripped as a single item. It then unpacks that sublist into a tuple (item[0], item[1], item[2]), thus turning your list of lists into a list of tuples.

(Haven’t covered tuples? For purposes of this discussion only, a tuple is like a list, except that it is immutable, once constructed. It is surrounded by parentheses, rather than brackets.)

Now, you have a list of tuples on the right, and three variables separated by commas on the left, so the parser looks at the list, and if it does not contain exactly three items, says: “too many items to unpack, (expected 3)”

# three items on left, iterable of length 3 on right
a, b, c = [(1,2,3), (4,5,6), (7,8,9)]  
print(a, b, c)

# three items on left, iterable of length 3 on right
x, y, z= [(1,2,3), (4,5,6), (7,8,9), (10, 11, 12)]  # three items on left, iterable of length 4 on right
print(x, y, z)

# Output:
(1, 2, 3) (4, 5, 6) (7, 8, 9)     
Traceback (most recent call last):
  File "C:\path\to\test.py", line 30, in <module>
    x, y, z= [(1,2,3), (4,5,6), (7,8,9), (10, 11, 12)]
ValueError: too many values to unpack (expected 3)

I don’t think that you can do what you are trying to do in a single comprehension. It’s good exercise to compose the three comprehensions, but If you just want one for loop, I think you’ll need to code it the conventional way.

titles = []
poets = []
dates = []
for item in highlighted_poems_details:
    titles.append(item[0])
    poets.append(item[1])
    dates.append(item[2])
1 Like

Thank you for the explanation! I understand now! If it had worked I would not have gotten the expected values, I would have had three lists each with a tuple and not three lists of like items. I tend to experiment with the skills and lessons and thought I could combine multiple variable assignment with lists too.

this answers my curiosity :smiley:

1 Like

thank you for explaining this, I tend to extrapolate concepts without knowing if I can actually do it. :grin:

1 Like

Thank you for clearing that up. I also didn’t know that about list aliasing :smiley:

1 Like