0

Double variable list comprehension in python

Can we , in list comprehension , abstract two variables ? like let's say we have a list of lists,A, and we need to create a list which contains the odd indexed members of all the lists in A . I tried the following : new_list = [ [ j[i] for j in matrix] for i in range(len(j)) and i % 2 == 0 ] but I am getting an error stating "j" is not defined . Can we have a solution in the above fashion where list comprehension with two variables is being used ?

11th Feb 2019, 1:08 PM
Kanishk Viman
Kanishk Viman - avatar
14 Réponses
+ 3
Kanishk Viman You're welcome! enumerate() creates a tuple for each element in a list which containts the element's index and the element itself: print(*enumerate(a)) # (0, [1, 9, 2, 6]) (1, [5, 1, 10, 7]) (2, [9, 1, 2, 4, 5]) (3, [9, 8, 9, 8, 3]) (4, [7, 4, 5, 8, 4]) (5, [7, 3, 5, 9, 6]) In a for loop, you can assign the index to the variable i and the element to the variable l like this: for i, l in enumerate(a): print(i, l) output: 0 [1, 9, 2, 6] 1 [5, 1, 10, 7] 2 [9, 1, 2, 4, 5] (...) I did the same thing in my list comprehension: b = [l for i, l in enumerate(a) if i%2] => for every element in a, assign the variable i to its index and l to the element and only take the element (l) if its index is odd (i%2). Maybe if the tuple is in parentheses it's a little more obvious what it does: b = [l for (i, l) in enumerate(a) if i%2]
11th Feb 2019, 1:37 PM
Anna
Anna - avatar
+ 1
You can, but that would overcomplicate what you're trying to do: a = [[1, 9, 2, 6], [5, 1, 10, 7], [9, 1, 2, 4, 5], [9, 8, 9, 8, 3], [7, 4, 5, 8, 4], [7, 3, 5, 9, 6]] b = [a[i] for i in range(len(a)) if i%2] print(b) # [[5, 1, 10, 7], [9, 8, 9, 8, 3], [7, 3, 5, 9, 6]] I'd probably do this: b = [l for i, l in enumerate(a) if i%2] Here's an example of an actually nested list comprehension: b = [[i, j] for i in range(2) for j in range(3)] print(b) # [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]] You can use as many variables and nest as many lists as you need.
11th Feb 2019, 1:18 PM
Anna
Anna - avatar
+ 1
This might be hard to understand but does the job (?). Let's wait for Anna answer. a = [[1,2,3] ,[4,5,6] , [7,8,9]] b = [i for x in ([c[i] for i in range(0, len(c), 2)] for c in a) for i in x] print(b) # [1, 3, 4, 6, 7, 9]
11th Feb 2019, 2:04 PM
Diego
Diego - avatar
+ 1
Kanishk Viman Did you see my last post? The output is exactly the same as the expected output in your post... 🤔
11th Feb 2019, 2:41 PM
Anna
Anna - avatar
0
@Anna : Thanks a ton :) But I need help with the syntax , [I for i ,...] ...how is it deduced that I for i will mean I[i] ?
11th Feb 2019, 1:29 PM
Kanishk Viman
Kanishk Viman - avatar
0
for a = [ [1,2,3] ,[4,5,6] , [7,8,9]] the expected output is : [1,3,4,6,7,9] but I am getting [4,5,6] with b = [l for i, l in enumerate(a) if i%2]
11th Feb 2019, 1:34 PM
Kanishk Viman
Kanishk Viman - avatar
0
Oh, okay. I understood "we need to create a list which contains the odd indexed members of all the lists in A" that the new list should only contain a[1], a[3] etc. Wait a minute...
11th Feb 2019, 1:39 PM
Anna
Anna - avatar
0
I'm playing around with something like this, but it looks pretty terrible: b = [m for i, m in enumerate([e for l in a for e in l]) if i%2]
11th Feb 2019, 2:09 PM
Anna
Anna - avatar
0
I misunderstood the question yet again 😅 Here you go: a = [[1,2,3], [4,5,6], [7,8,9]] b = [e for l in a for i, e in enumerate(l) if i%2 == 0] print(b) # [1, 3, 4, 6, 7, 9]
11th Feb 2019, 2:16 PM
Anna
Anna - avatar
0
@Anna : Thanks a lot again :) ..I understood the logic now , afraid that it might not make a list out of all the odd elements of the lists contained in the initial list
11th Feb 2019, 2:21 PM
Kanishk Viman
Kanishk Viman - avatar
0
@Diego : This solution worked . But I see you have created a generator there... any reason why a generator but not a list ?
11th Feb 2019, 2:26 PM
Kanishk Viman
Kanishk Viman - avatar
0
@diego : Works with lists as well , thanks a lot :)
11th Feb 2019, 2:28 PM
Kanishk Viman
Kanishk Viman - avatar
0
Kanishk Viman I'm more used to generators.
11th Feb 2019, 2:42 PM
Diego
Diego - avatar
0
@Anna : Sorry Anna ..missed it somehow ... Thanks a lot ...Apologies again
11th Feb 2019, 5:59 PM
Kanishk Viman
Kanishk Viman - avatar