-3

I have a list of unique elements. I want to break it down into several lists so that each list has only one element of the original list and has a unique name based on the element index separately. Is there an idea to solve this issue?

lst = ['1', '2', '3']

the output must be:

lst_0 = ['1']
lst_1 = ['2']
lst_2 = ['3']
Ali
  • 115
  • 5
  • 2
    Hello. Can you show the code you have attempted so far to help point out where you are having issues exactly? Also, are you looking to create an arbitrary number of variables based on a varying length list? Or is it always a list of size 3? – idjaw Jun 02 '22 at 14:13
  • 4
    Sound like an [x/y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), could you explain *why* you want to do this? – 0stone0 Jun 02 '22 at 14:14
  • 2
    First of all, you don't really want separate variables. See [How do I create variable variables?](https://stackoverflow.com/q/1373164/6045800). You want a list of lists. And that's pretty straight-forward: `res = [[x] for x in lst]` – Tomerikoo Jun 02 '22 at 14:16
  • Even then. If you want a variable number of variables that are representing elements of a list somehow, the answer here really is to use a dictionary instead which is explained in the link already provided in the previous comment [here](https://stackoverflow.com/a/1373185/1832539) – idjaw Jun 02 '22 at 14:27
  • @idjaw Why to use a dictionary when the keys are just sequential integers (disregarding the meaningfulness of the `lst_` prefix)? That's just a glorified list... – Tomerikoo Jun 02 '22 at 14:42
  • @Tomerikoo - It all depends. In this example it is a sequential list. What do you do when it isn't sequential and you want a specific item in that list that you can want/need to get in `O(1)` time? I think overall it speaks to the clarity required in the question to understand what OP is really looking to do. – idjaw Jun 02 '22 at 15:42

2 Answers2

2

Avoid variable names like this. Instead, use a dictionary like the one below. Initialize it using dictionary comprehension and enumerate:

lst = ['1', '2', '3']
dct = {f'lst_{i+1}': [x] for i, x in enumerate(lst)}
print(dct)
# {'lst_1': ['1'], 'lst_2': ['2'], 'lst_3': ['3']}

print(dct['lst_1'])
# ['1']

Or, if you want a more intuitive and, perhaps, more fitting data structure, use a list of lists, as suggsted in the comment by Tomerikoo. Initialize it using list comprehension.

lst_of_lsts = [[x] for x in lst]
print(lst_of_lsts)
# [['1'], ['2'], ['3']]
print(lst_of_lsts[0])
# ['1']

SEE ALSO:

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 1
    You can just do `enumerate(lst, 1)` to avoid the repeated additions. But also, this is hardly a use-case for a dict as the keys are non-unique strings of sequential nature. This really calls for a simple list-of-lists... – Tomerikoo Jun 02 '22 at 14:21
  • @matszwecja: Thx! Updated. I misread the question... – Timur Shtatland Jun 02 '22 at 14:21
  • I wouldn't link the 2nd question. Most voted answer doesn't have enough of a disclaimer why exec/eval is a terribly bad idea in that case. – matszwecja Jun 02 '22 at 14:22
  • 1
    Simplify further - `dct = {f'lst_{i}': [x] for i, x in enumerate(lst, 1)}` – Daniel Hao Jun 02 '22 at 14:22
1

the output must be:

lst_1 = ['1']
lst_2 = ['2']
lst_3 = ['3']

So you want to output all the elements in your list with a particular format, right?

for index, value in enumerate(lst):
    print(f"lst_{index} = ['{value}']")

This will give you exactly the expected output.


If you really want to create a list of lists out of a list, you can do this:

>>> print([[x] for x in lst])
[['1'], ['2'], ['3']]

For sure there are far more elegant ways of doing this, I'm just answering the question as it is.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28