-4
Lst = [[1,2,3],[201,202,203],[3,1,4],[591,2019,3.14]]

What I need is-

a1,b1,c1 = (1,2,3)

a2,b2,c2 = (201,202,203)

and so on ...

taras
  • 6,566
  • 10
  • 39
  • 50
user9916003
  • 15
  • 1
  • 1
  • Related: [python - Unpacking, extended unpacking, and nested extended unpacking - Stack Overflow](https://stackoverflow.com/questions/6967632/unpacking-extended-unpacking-and-nested-extended-unpacking) – user202729 Jul 28 '18 at 13:17

4 Answers4

3

You are a looking to map values to coordinates. Well, you can just access directly via L[outer_list_idx][inner_list_idx] with a couple of list operations.

For non-numeric indexing, use dict

You can use a dictionary for a variable number of variables. This removes the need to define a large number of variables explicitly.

Using letters has the obvious limitation of allowing a maximum of 26 lists. If this is what you wish, there's no need to define letters explicitly, use string.ascii_lowercase:

from string import ascii_lowercase

L = [[1,2,3],[201,202,203],[3,1,4],[591,2019,3.14]]

m, n = len(L), len(L[0])

d = {(ascii_lowercase[j], i+1): L[i][j] for i in range(m) for j in range(n)}

print(d)

{('a', 1): 1, ('b', 1): 2, ('c', 1): 3,
 ('a', 2): 201, ('b', 2): 202, ('c', 2): 203,
 ('a', 3): 3, ('b', 3): 1, ('c', 3): 4,
 ('a', 4): 591, ('b', 4): 2019, ('c', 4): 3.14}

Then access via tuple keys. So, to retrieve the 2nd value from the 3rd list, you can use d[('b', 3)].

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    I suppose that you're thinking that OP has an XY problem and try to solve the X. – user202729 Jul 28 '18 at 13:11
  • 1
    You probably meant "downvoting a **good** answer for a bad question". Although if it's a duplicate, people should just answer the duplicate target. – user202729 Jul 28 '18 at 13:15
  • My example wasn't the *best* because I use numeric coordinates. You can, fairly easily, also use `a` instead of `1` etc. – jpp Jul 28 '18 at 13:21
1

The example is can be solved with simple tuple unpacking. You can use zip to pair your values:

lst = [[1,2,3],[201,202,203],[3,1,4],[591,2019,3.14]]

((a1, b1, c1), (a2, b2, c2)), _ = zip(lst[::2], lst[1::2])
print(a1, b1, c1)
print(a2, b2, c2)

This will print:

1 2 3
201 202 203

If you want next values, you can do:

_, ((a1, b1, c1), (a2, b2, c2)) = zip(lst[::2], lst[1::2])
print(a1, b1, c1)
print(a2, b2, c2)

Output:

3 1 4
591 2019 3.14
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

You can use itertools.chain.from_iterable() for this:

t = [[1,2,3],[201,202,203],[3,1,4],[591,2019,3.14]]

from itertools import chain 
a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4 = chain.from_iterable(t) # flattens the list of lists

print(a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4)

Output:

 1 2 3 201 202 203 3 1 4 591 2019 3.14

You need to know the dimensions of the list beforehand and the variablenames. I would probably just use the original list ...

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

Do you just need the values? Then like that:

Lst = [[1,2,3],[201,202,203],[3,1,4],[591,2019,3.14]]

for k in Lst:

    a, b, c = k
    print(a, b, c)
Joe
  • 6,758
  • 2
  • 26
  • 47