1

I am a newbie. I have created a list from a .csv file:

 h = [2.3, 1.4, 4.5, 4.5, 1.4, 2.3]

The number of items in the list varies (from 2 to 36) depending on the .csv file. I want to create incremental variables from the list like this (so I can use them later in the code):

 L1 = 2.3
 L2 = 1.4
 L3 = 4.5
 L4 = 4.5
 L5 = 1.4
 L6 = 2.3

My problem is that the number of items in the list from the .csv file varies and I have tried using the increment and enumerate methods, but I cannot make it work at all.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
CPMAN
  • 69
  • 6
  • 2
    what do you mean by *incremental variable*? Why can't you use indexing to access elements of list? – Sociopath Apr 05 '19 at 10:49
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Thierry Lathuille Apr 05 '19 at 11:04
  • Thank you for responding. I will try to explain my ignorance which is a self-referencing situation. :-) I want to loop through the list and create a unique variable for each item. But since I don't know how many items are in the list, I don't know how many unique variables to create. I am thinking I should loop through the list and create the variables, but I don't know how to make each variable unique. – CPMAN Apr 05 '19 at 11:09
  • 1
    Short answer: you shouldn't. Use a dict or something similar. Have a look at the duplicate for reasons why and ideas of what to do instead. – Thierry Lathuille Apr 05 '19 at 11:15
  • This seems like an [xy problem](http://xyproblem.info/). What are you trying to accomplish with *sequential variables* that indexing can't accomplish? – TrebledJ Apr 05 '19 at 11:24

3 Answers3

2

If I understand you correctly, it would help to create a dict from the list.
You can use this dict comprehension statement.

h = [2.3, 1.4, 4.5, 4.5, 1.4, 2.3]

result = {f"L{index+1}": value for index, value in enumerate(h)}

> {'L1': 2.3, 'L2': 1.4, 'L3': 4.5, 'L4': 4.5, 'L5': 1.4, 'L6': 2.3}

And you can get the number which you need by specifying the key. For example:

print(result["L1"])

> 2.3
  • Thank you DenisOH, this will work for my situation. I apologize to all the others to whom I did not explain well enough what I wanted to accomplish. – CPMAN Apr 05 '19 at 11:44
0

Instead of defining one variable for each element in the list, you can simply get the value of each element via its index:

h[0] # first element of the list (2.3)
h[1] # second element of the list (1.4)
h[2] # third element of the list (4.5)
# etc

which you can use like print(h[0]).

len(h) gives you the number of elements in h. The last element in the list has index len(h)-1 (5 in your example).

glhr
  • 4,439
  • 1
  • 15
  • 26
-1

I would suggest you stick with the list for storing data , and access it via h[i]

However , you can still have your wish using exec:

for i,x in enumerate(h):
    exec(f'L{i}=x')

Please note that the first L will be L0 , change the above code if you want to start with 1.

Born Tbe Wasted
  • 610
  • 3
  • 13
  • I get this error: eval(f'L{i} = x') File "", line 1 L0 = x ^ SyntaxError: invalid syntax – CPMAN Apr 05 '19 at 10:59
  • Nevermind I can't use eval (https://stackoverflow.com/questions/5599283/how-can-i-assign-the-value-of-a-variable-using-eval-in-python) – Born Tbe Wasted Apr 05 '19 at 11:00