0

I have assigned sub lists a name from a large list, as followed:

AB, CD, EF, GH, IJ, KL, MN, OP, QR, ST, UV, WQ, YZ, AA, BB, CC, DD, EE, FF, GG = large_list[0], large_list[1], large_list[2], large_list[3], large_list[4], large_list[5], large_list[6], large_list[7], large_list[8], large_list[9], large_list[10], large_list[11], large_list[12], large_list[13], large_list[14], large_list[15], large_list[16], large_list[17], large_list[18], large_list[19]

This seems pretty unpractical, hence was wondering whether there is a method that is considered more efficient or even shorter for that matter.

I'm assuming some form a loop would help? e.g. for sub_list in large_list: ???

Enigmatic
  • 3,902
  • 6
  • 26
  • 48
  • Does the left side of the assignment also need to be automated? The left side is pretty easy as Python does it for you automatically – Adirio Dec 20 '16 at 10:27

3 Answers3

2

You can use Python's unpacking abilities:

L = list(range(10))
a, b = L[:2]

For your case:

AB, CD, EF, GH, IJ, KL, MN, OP, QR, ST, UV, WQ, YZ, AA, BB, CC, DD, EE, FF, GG  = large_list[:20]

For better readability, you can break lines, using parenthesis:

large_list = list(range(100))
(AB, CD, EF, GH, IJ, KL, MN, OP, QR, ST, UV, WQ, 
 YZ, AA, BB, CC, DD, EE, FF, GG)  = large_list[:20]

Now AB is 0 and GG is 19

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • @AriGold That wouldn't help, the point is to truncate the list so it's only as long as the number of variables being assigned to (which is 20). If `large_list` is longer than 20 then you want to reduce it to 20 items. – SuperBiasedMan Dec 20 '16 at 10:29
  • Why is `list(range(100))` necessary? It seems to work fine without. – Enigmatic Dec 20 '16 at 10:33
  • I am using Python 3. While you can slice `range` objects, the OP asked for a list. So it works without `list` but makes it closer the question with it. In Python 2 `range() ` returns a list. Converting into a list does not hurt. – Mike Müller Dec 20 '16 at 10:36
1

I would have use a dictionary for that

li = ["AB", "CD", "EF", "GH", "IJ", "KL", "MN", "OP", "QR", "ST", "UV", "WQ", "YZ", "AA", "BB", "CC", "DD", "EE", "FF", "GG" ]
dicti = {}
for subList,key in zip(large_list,li):
  dicti[key] = subList

you can use dict comprehension as well:

li = ["AB", "CD", "EF", "GH", "IJ", "KL", "MN", "OP", "QR", "ST", "UV", "WQ", "YZ", "AA", "BB", "CC", "DD", "EE", "FF", "GG" ]
dicti = dicti = {key: sub_ist for key, sub_ist in zip(li, large_list)}

As Chris_Rands wrote at the comment, you can simply do:

dict(zip(li,large_list))
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
1

Python has something called tuple unpacking, where you can assign multiple values from a collection. It 'unpacks' the collection to turn it into all its parts. Here's a simple example:

numbers = [1, 2, 3]
a, b, c = numbers
# a, b, c = [1, 2, 3]
# a = 1, b = 2, c = 3

So you can do your method just by having the list name on the right:

AB, CD, EF, GH, IJ, KL, MN, OP, QR, ST, UV, WQ, YZ, AA, BB, CC, DD, EE, FF, GG = large_list

Though if large_list has more or less items in it than you're assigning to, you will get errors. (ie. if large_list has 21 items but you're trying to assign to 20). You can mitigate it being too large by using large_list[:20] to at least only take the first 20 items in the list.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73