2

I want to save my list to numbered variable.

myList =[1,2,3,4,5]

and I want save be like this :

numb1 = myList[1]
numb2 = myList[2]
numb3 = myList[3]
numb4 = myList[4]
numb5 = myList[5]

I don't want do it manually, because myList contains many elements. Can you give me suggestion?

Imam Abdul Mahmudi
  • 293
  • 2
  • 5
  • 16
  • 1
    One problem you'll have is that Python list indexing begins at 0, not 1. So in your example, you are ignoring the first element, `myList[0]`, and you'll get an index out-of-range error when attempting to access `mylist[5]`, which doesn't exist. – Tom Karzes Dec 15 '15 at 03:35
  • 3
    "I want to save my list to numbered variable." This defeats the _entire purpose_ of the `list` type. – TigerhawkT3 Dec 15 '15 at 03:45
  • I can't even close this as a duplicate of ["variable variables"](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python) because at least that one wanted to use heterogeneous names. This, however, is just a fake `list`. There is no reason to do any of this. Use a `list`. – TigerhawkT3 Dec 15 '15 at 03:56

2 Answers2

2

You could simply do this via enumerate():

>>> myList =[1,2,3,4,5,6,7,8,9,10]
>>> for index, value in enumerate(myList, start=1):
...     globals()['numb'+str(index)] = value
...     
... 
>>> numb1
1
>>> numb2
2
>>> numb3
3
>>> numb10
10
>>> 

But I'd recommend use another dict instead of globals():

>>> myList =[1,2,3,4,5,6,7,8,9,10]
>>> d = {}
>>> for index, value in enumerate(myList, start=1):
...     d['numb'+str(index)] = value
...     
... 
>>> d['numb1']
1
>>> d['numb2']
2
>>> d['numb10']
10
>>> 
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • Of course there's still the question of whether d['numb10'] has any advantage over myList[9]. – Jayanth Koushik Dec 15 '15 at 03:49
  • @JayanthKoushik: I think that's an OP's typo, however `numb5 = myList[5]` raise an error in this case as Tom said in comments. I'm sure that OP wants `numb5 = myList[4]` which gives `numb5 == 5`. Also `numb1 = myList[0]` which gives `numb1 == 1`. – Remi Guan Dec 15 '15 at 03:51
  • @JayanthKoushik - Indeed, there is no advantage to doing that. There are only disadvantages. Referring to a series of homogeneous objects is the whole point of a `list`. – TigerhawkT3 Dec 15 '15 at 03:53
  • Using `globals()` to create a series of numbered references in a horrifying mimicry of a `list` is generally a good indicator that something is very wrong. – TigerhawkT3 Dec 15 '15 at 03:55
  • @TigerhawkT3: I'd say so, so if OP really want to `'numb1'` to get `myList[1]`, then use another dict instead of `globals()` as I said in answer. – Remi Guan Dec 15 '15 at 03:56
  • It's numbered. That is a `list`. If they want to use the phrase `numb` in there, they should make a new reference `numb = myList` and then they can do `numb[0]`. If they want the indexing to start with `1` (for no good reason), they can put `None` as the first element with `numb = [None] + myList`. – TigerhawkT3 Dec 15 '15 at 03:58
  • @TigerhawkT3: Ah, however no idea about what's OP trying to do. But, isn't it a good idea about use a dict instead of `numb = [None] + myList` if OP wants use `numb1` to get `myList[0]`? I think it would be little slow if the list is large. – Remi Guan Dec 15 '15 at 04:05
1

This is an XY Problem in which problem X (i.e., referring to elements in a list) is much easier than problem Y (i.e., creating a series of numbered variables corresponding to elements in a list). On top of that, doing this would defeat the entire purpose of using a list. It doesn't take too long to hard-code it for the five elements you showed, but the list with "many elements" you mention should not be given this treatment.

A list holds several objects, generally of a homogeneous type and purpose, like inches of rainfall on each day of the year. This data structure makes it easy to store and process all of these objects together. You can also refer to them individually, by indexing.

Where you are hoping to refer to the first element of myList as numb1, use instead myList[0]. That is the reference that you already have for that object. You do not need to pollute the namespace with a pile of numbered references. The list type is intended precisely for this situation. Please use a list.

Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97