0

This post follows my previous question from here: Split list into different variables

I realized that I would have multiple chunks of elements in a list and these may not always be restricted to 3. For example, I could have:

[('love', 'yes', 'no'), ('valentine', 'no', 'yes'), ('day', 'yes','yes'), 
   ('house', 'yes','no'), ('car', 'yes','yes')]

[('love', 'yes', 'no'), ('valentine', 'no', 'yes'), ('day', 'yes','yes'), 
   ('house', 'yes','no'), ('car', 'yes','yes'), ('balloon', 'no','no'), 
   ('roses', 'yes','yes')]

var1, var2, var3 = listobj works fine for only three of these element chunks. But for more, in general, how would I store all these chunks of elements into different variables?

Community
  • 1
  • 1
Eagle
  • 1,187
  • 5
  • 22
  • 40
  • Do you really need to store all elements in _different_ variables? Isn't it possible to select an element from a list? – devnull Feb 15 '14 at 16:13
  • Yup, that would make it easier for me to work with the chunks of data – Eagle Feb 15 '14 at 16:15
  • No it wouldn’t. Because as you say: You don’t always have three elements, so you would have to deal with a variable number of variables. That’s not easier. – poke Feb 15 '14 at 16:26
  • @poke Well, for my application purposes, I would need to store them separately. I used `allCols = [[] for x in range(0, colCount)]` where `colCount` is the number of columns in the row. Indexing them later should not be a problem, and I don't mind indexing the chunks obtained from the list. – Eagle Feb 15 '14 at 16:32
  • see https://stackoverflow.com/questions/10299682/how-to-unpack-tuple-of-length-n-to-mn-variables – n611x007 Dec 12 '14 at 15:22

2 Answers2

4

The variables are declared explicitly in your code, so the numbers of variables you could use is definite, there is no way to unpack a tuple(or list) with unknown length to definite numbers of vars. But you can use * to make extended iterable unpacking in python3:

>>> a, *b, c = range(5)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Good idea. However, I solved my problem using this line: `allCols = [[] for x in range(0, colCount)]` where `colCount` is the number of columns in the row. Then, I just indicate the index that I want to access in `allCols[colCount-1]` to extract the last element set or any set for that matter. – Eagle Feb 15 '14 at 16:22
  • @Eagle yes that's the common way to refer to elements in an iterable – zhangxaochen Feb 15 '14 at 16:30
0

If the length of listobj is indeterminate, use it directly as a list instead of unpacking it into variables.

Unpacking a list of different sizes doesn't make any sense. Assume you could dynamically unpack listobj into as many variables as possible (like var1, var2, var3... as long as needed). How would you write code that consumes these variables? You'd suddenly need to know their number and some clever way to index a particular one, or loop over them. And guess what- this is exactly why you have lists in the first place.

Kos
  • 70,399
  • 25
  • 169
  • 233