Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add all variables to a list
#1
hi guy, i'm really struggeling cause i don't find something to add all Variables to a list at once.

var_one = 1
var_two = 2
var_three = 3
var_four = 4
var_five = 5
var_six = 6
var_7 = 7
var_8 = 8
var_nine = 9


list = []
thanks for helping me!
Reply
#2
first of all, how do you create these variables? Second, you really shouldn't be creating variable names dynamically like this.
http://stupidpythonideas.blogspot.com/20...reate.html

as a side note - don't use list as name, it's a built-in function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Are you sure you want to put a bunch of variables in a list, or do you just want a list? You could do this:
myvars = [var_one, var_two,... var_nine]
But that is identical to :
my_vars = [1, 2, 3, 4, 5, 6, 7, 8, 9]
By identical I mean the list will contain the value of these variables, because these variables are immutable (cannot be changed). If, after creating the list, I did this:
var_one = 101
Now var_1 == 101, but my_list[0] == 1. The reason for this is obvious and completely unclear depending on how long you have been writing python

When you type "var_one = 1" you are not telling python "I want to use the name "var_one" to mean 1. Python adds the name and value to a dictionary so it can look up the value if you reference "var_one" elsewhere in your program. When you add "var_one" to a list Python says "Ah! someone wants to know what var_one means. I will look it up and return the value I have saved". So the thing that gets added to the list is the value Python has saved for var_one. If later on I type "var_one = 2", Python does the same 'save the value referenced by a name' thing. This time Python sees that "var_one" is already in the dictionary, so it reuses the name and saves the new value.

That's why I am a little confused when you ask to add a bunch of variables to a list. Why do that? There are valid reasons. For example I might save a bunch of variables to a list as a kind of shapshot. The values in the list would not change, even if the variables used to create the list do.

Another reason for creating a list of variables is that the variables are mutable. A mutable object does not change, but it's value does. A list is mutable If I do this:
mylist = [1, 2, 3]
mylist.append(4)
mylist.remove(2)
mylist is the same "variable" after each of the commands is executed. If you look in the python dictionary under "mylist" you will see the identifier for the list (essentially his storage location) is unchanged. What does change with each command is the value(s) stored inside mylist.

A list of mutable variables can be a nice way to organize your code. Earlier there was a question on the forum about finding available choices for each square in sudoku puzzle. The standard sudoku puzzle has 81 mutable variables, one for each "cell" in the 9x9 sudoku board. The sudoku program used a list for each row in the puzzle and another array to hold all the lists. Thus the sudoku board could be stored as a list of mutable objects, and it was a lot easier to look at a cell using cell[row][col] than having 81 variables named cell11, cell12...

What are you planning to do with your list of variables?
Reply
#4
thank for the answer!

I know that the list shows the value of the variable, thats just a simple example here with no meaning.

i just wanted to know: is there a command to add all variables at once into a list. not by tiping the value into the list or by adding the variable by list.appen.
Reply
#5
(Apr-13-2020, 09:42 PM)faszination_92 Wrote: i just wanted to know: is there a command to add all variables at once into a list. not by tiping the value into the list or by adding the variable by list.appen.

How would you specify which variables you wanted in the list without typing them in? Can you give an example of how you might want it to work?
Reply
#6
dir() will return a list of all "things" you can see from the current context. If your variable names followed a pattern you could search through the items for names matching the pattern and add them to your list. This would be really strange and I have a hard time thinking why you would do so.
Reply
#7
(Apr-14-2020, 03:51 AM)deanhystad Wrote: dir() will return a list of all "things" you can see from the current context. If your variable names followed a pattern you could search through the items for names matching the pattern and add them to your list
I think you should refer to globals() and locals(), not dir()
Still, bunch of [variables'] names "matching a pattern" suggest you should be considering a data structure instead.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 499 May-13-2023, 03:30 AM
Last Post: knight2000
  Converting list to variables Palves 1 1,734 Sep-18-2020, 05:43 PM
Last Post: stullis
  Print variable values from a list of variables xnightwingx 3 2,573 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Using a list of variables Zane217 6 2,478 Jun-09-2020, 01:37 PM
Last Post: Yoriz
  Creating a List with many variables in a simple way donnertrud 1 2,014 Jan-11-2020, 03:00 PM
Last Post: Clunk_Head
  2D Array/List OR using variables in other variable names? IAMK 4 3,796 Apr-16-2018, 09:09 PM
Last Post: IAMK
  list vs variables mcmxl22 2 3,122 Jan-27-2018, 10:00 AM
Last Post: Gribouillis
  list of user's variables in the interpreter nzcan 5 3,854 Jan-21-2018, 11:02 AM
Last Post: nzcan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020