0

I am going through David Kopec's Classic Computer Science Problems in Python and in chapter 3 - came across this list comprehension.

https://github.com/davecom/ClassicComputerScienceProblemsInPython/blob/master/Chapter3/word_search.py

all_locations = [locs for values in assignment.values() for locs in values]

I am unable to understand how exactly this list comprehension works.

From this answer for 'Double iteration in List' - it looks like I could change the names of the variables in the 2nd part of the list comprehension but that just breaks it.

Double Iteration in List Comprehension

I wanted to understand which is the first item that is getting iterated over and which is the next. The end result is a compacted list but am scratching my head over - how ??

This is the sample code I tried

class GridLocation(NamedTuple):
    row: int
    column: int

gl_1 = GridLocation(1, 1)
gl_2 = GridLocation(2, 2)
gl_3 = GridLocation(3, 3)

test = {'1' : [[gl_1, gl_2]], '2' : [[gl_2, gl_3]], '3': [[gl_3, gl_1]]}

print('test.values() -> ', test.values())            
print('-' * 25 )
test_list_comp = [locs for values in test.values() for locs in values]
print('test_list_comp -> ', test_list_comp)

This is the result -

test.values() -> dict_values([
[[GridLocation(row=1, column=1), GridLocation(row=2, column=2)]], 
[[GridLocation(row=2, column=2), GridLocation(row=3, column=3)]], 
[[GridLocation(row=3, column=3), GridLocation(row=1, column=1)]]
])

-------------------------

test_list_comp -> [
[GridLocation(row=1, column=1), GridLocation(row=2, column=2)], 
[GridLocation(row=2, column=2), GridLocation(row=3, column=3)], 
[GridLocation(row=3, column=3), GridLocation(row=1, column=1)]
]

Vidya
  • 17
  • 1
  • 6

1 Answers1

0
test_list_comp = [locs for values in test.values() for locs in values]

Is equivalent to:

test_list_comp = []

for values in test.values():
    for locs in values:
        test_list_comp.append(locs)

List comprehensions are evaluated from left to right.

Dipen Dadhaniya
  • 3,830
  • 2
  • 7
  • 14
  • Got the first part, but did not get "And yet the variable name for locs has to be same throughout. Why ?" Please, can you elaborate on this? – Dipen Dadhaniya Sep 15 '19 at 08:29
  • So each value in test.values() - 1st iteration - would of form - `[[GridLocation(row=1, column=1), GridLocation(row=2, column=2)]] ==> [[]]` and then - locs in values - 2nd iteration - would be `[GridLocation(row=1, column=1), GridLocation(row=2, column=2)] => []` And yet the variable name for locs has to be same throughout. Why ? – Vidya Sep 15 '19 at 08:37
  • In the stackoverflow link that I have mentioned in the question - it is written 'Additionally, you could use just the same variable for the member of the input list which is currently accessed and for the element inside this member. However, this might even make it more (list) incomprehensible.' - indicating one can use different variables for 1st and 2nd part of the list comprehension. However when I tried it - It did not work. – Vidya Sep 15 '19 at 08:41
  • What the answer is trying to say is that we can do "test_list_comp = [x for x in test.values() for x in x]". What you are trying exactly? – Dipen Dadhaniya Sep 15 '19 at 09:05
  • test_list_comp = [x for x in test.values() for x in x] - trying to change x in the 2nd part of the list comp. – Vidya Sep 16 '19 at 04:45
  • We can't do that: We can do "test_list_comp = [locs for x in test.values() for locs in x]". – Dipen Dadhaniya Sep 16 '19 at 04:49