-1
VC_I=[1,2,4,3]

for i in range(len(VC_I)):
  print(VC_i=[])

TypeError: 'VC_i' is an invalid keyword argument for this function

Jawad
  • 11,028
  • 3
  • 24
  • 37
  • What exactly are you trying to ask here? It is not really clear with the title and the description. Going with the description the code is incorrect probably you want to try this `for i in range(len(VC_I)): print(VC_I[i])` – AmeyaVS Jan 30 '20 at 05:07
  • No, I want to create lists having names according to i in the loop – Vinita Ramnani Jan 30 '20 at 05:08
  • You have to format it in the output yourself: `print(f'vc[{i}]={vc[i]}')`. Even the new "debug format string" print(f'{vc[i]=}') won't work because it prints the symbolic expression without any interpolation so it's going to print `vc[i]=1` rather than `vc[0]=1`. – Masklinn Jan 30 '20 at 07:11

4 Answers4

0

Why dont you just use a dictionary for this purpose?

VC_I=[1,2,4,3]
x = {}
for i in range(len(VC_I)):
    x[VC_I[i]] = [value, you, want]

To access use:

print(x["key name"])

To do what you wanted,

x = dict(zip(VC_I, PAYLOAD_IN))
Eriall
  • 177
  • 1
  • 7
  • I can't bcuz I need to associate multiple values to that VC_i – Vinita Ramnani Jan 30 '20 at 05:03
  • So what you can do is simply, x[VC_I[i]] = ['your', 'values', 'here'] – Eriall Jan 30 '20 at 05:05
  • At the end I want something like this: VC_1=[1234ead,345eef,4567ea,123aaa], similarly VC_2=[] and so on... – Vinita Ramnani Jan 30 '20 at 05:05
  • I want them as lists for a reason – Vinita Ramnani Jan 30 '20 at 05:06
  • 2
    They stay as lists when assigned to a dictionary, you simply need to call the dictionary name and key (variable name) that you want to access. i've added an example at the bottom of my answer. You might want to go here https://docs.python.org/3/tutorial/datastructures.html#dictionaries and read about what a dictionary is... – Eriall Jan 30 '20 at 05:09
  • I want to do something like this: I have got a list of VC_IN=[1,3,65,13,4,1,65,3] and a list of PAYLOAD_IN=[ 'a2','123','21','1a','ae','23','12','ef'] where PAYLOAD_IN[0] corresponds to VC_IN[0] and so on. Now I will find the different VC's from VC_IN that is 1,3,65,13,4 and want to create lists with the name VC_1, VC_3, VC_65 and so on and their respective payloads will be the data for the lists, now can you tell me how to exactly do this? – Vinita Ramnani Jan 30 '20 at 06:04
  • see my latest revision, it still uses dictionaries, which i still dont think you understand how they work. – Eriall Jan 30 '20 at 06:25
0

You need to use special module to get variable name. See here: Getting the name of a variable as a string

BTW, to make your example correctly iterating just the values do the following:

VC_I=[1,2,4,3]
for i in range(len(VC_I)): print(VC_I[i])
Poe Dator
  • 4,535
  • 2
  • 14
  • 35
0

I don't know if you need this, but you can use the locals() to do this. For example, given your code:

VC_I=[1,2,4,3]
names_dict = dict(locals()) #Preserve a copy of all local variables
for key, val in names_dict.items():
    if val == VC_I:
        print('The variable name is:', key)

which will output, as you have guessed:

The variable name is: VC_I

However, this will only work if you are working with local variables. It also wouldn't work for variables with the exact same values. Take note.

edit You can even do this:

for key, val in names_dict.items():
    if val == VC_I:
        print(key, ' = ', val)

which should give you:

VC_I = [1, 2, 3, 4]
Seraph Wedd
  • 864
  • 6
  • 14
0

I think you want to create list like VC_1, VC_2, ... check this code

VC_I=[1,2,4,3,4,5,6,7,8]
PAYLOAD_IN=[ 'a2','123','21','1a','ae','23','12','ef'] 

for i in VC_I:
  name = 'VC_'+str(i)        
  print(name)                # here name variable is string
  name = list()
  print(name)                # here name variable is list
  print(type(name))          # checking data type of variable name

Answer for your specific question

for i, j in zip(VC_I, PAYLOAD_IN):
  name = 'VC_' + str(i)
  name = list()
  name.append(j)
  print(name)
Vikas Gautam
  • 441
  • 8
  • 22
  • I want to do something like this: I have got a list of VC_IN=[1,3,65,13,4,1,65,3] and a list of PAYLOAD_IN=[ 'a2','123','21','1a','ae','23','12','ef'] where PAYLOAD_IN[0] corresponds to VC_IN[0] and so on. Now I will find the different VC's from VC_IN that is 1,3,65,13,4 and want to create lists with the name VC_1, VC_3, VC_65 and so on and their respective payloads will be the data for the lists, now can you tell me how to exactly do this? – Vinita Ramnani Jan 30 '20 at 07:13
  • @Vinita hi upvote or mark this as answer if this solved your problem. – Vikas Gautam Feb 09 '20 at 12:43