0
x=["a","b","c","d","e"]

for i in x:
    ai=[i]
    print(ai)

print(ae)    

So for the above code, I am consufed, how can I call back these kind of lists. I mean for each i in the list x. I am defining "ai". Since last step is finishing with "e" what is the list a"e", or what become of my list created with a and e(from the list). How can I call a"e"=['e']? it is clearly not a"e" I tried to call as print(ae),print(a'e'), print(a"e") etc...

  • So, you are trying to dynamically create variables. Don't do that! Instead, use a *container* like a `list` or a `dict` – juanpa.arrivillaga Mar 27 '22 at 19:53
  • The variable in the loop is named `ai`. It is completely unrelated to the variable `i`. If you were expecting to get variables named `aa`, `ab`, etc, that's not how it works. – John Gordon Mar 27 '22 at 19:53
  • So, what happened in your code is that `ai=[i]` assigns to the variables `ai` a new list created by the expression `[i]`. On each iteration (except the first) the previous list that was being referenced by `ai` is no longer referenced and discarded. – juanpa.arrivillaga Mar 27 '22 at 19:54
  • yes but I can correct it and get whole variables but my basic question is in the last step how can we print our list what is it called? By the way given link to my question is not helpfull, Yes I know I can do it in other ways but I am curious what is it called.(I cannot refer the computer scientists notation sorry about that) – User not found Mar 27 '22 at 19:57
  • @JohnGordon So I dont expect anything or I dont ask how to do it correctly, I wrote a code and it worked so it means in for it loop it correctly with some variables so I just wonder what are these created variables. And to close question you should ask to the op why he asked the question. At least for as far as I know they do it in mse – User not found Mar 27 '22 at 20:08
  • I explained, you are trying to *dynamically create a variable*, and that **every list you created except the last one has been garbage collected**. The only list that remains is being referenced by the *single variable* you created `ai`. In CPython, you can dynamically create global variables (**but you shouldn't**) but not local variables. – juanpa.arrivillaga Mar 27 '22 at 20:09
  • there is a *single variable*, not multiple variables... what is mse? – juanpa.arrivillaga Mar 27 '22 at 20:11
  • yes I know there is only last variable on the memory, I can also call back ai just print(ai) my main point is: is there something related to "e" that I can call it like print(a'e')(yes I know it doesnot work but something like that) Probably I am just 1 point you dont read my comments seriously because I got your point. So since ai is dynamical we cannot call it with local variables from the list? But then why print(ai) brings something? so ai must refer something in the memory, isnot there a way to now the name of the variable? – User not found Mar 27 '22 at 20:18
  • "But then why print(ai) brings something? so ai must refer something in the memory, isnot there a way to now the name of the variable?" It is really difficult to understand what you are asking. The name of the variable is `ai`... but you seem to understand this. – juanpa.arrivillaga Mar 27 '22 at 20:31
  • "So since ai is dynamical we cannot call it with local variables from the list?" No, `ai` is *not dynamic*, it is being statically defined in source code when you wrote `ai = ...`, you can abuse things like `exec` to dynamically execute source code, or modify the `globals` dict directly. But this is a bad practice and a hack. Stop thinking in terms of *variables*. Variables are there for the person reading/writing the code, your program should not care about variables or their names. Think about *objects and data structures* – juanpa.arrivillaga Mar 27 '22 at 20:34

0 Answers0