Python Forum
Print variable values from a list of variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print variable values from a list of variables
Sorry, I'm new to python and coding in general. I been searching for some time and haven't found a way to do this.

This is the code so far:
vin1 = 100
vin2 = 200
vin3 = 300
vin4 = 400

vin_numbers = ['vin1', 'vin2', 'vin3', 'vin4']
print('\nAvailable VIN numbers:')
for vin in vin_numbers:
	print(f'{vin} = ')
I'm looking for a way to output the numeric values of the variables in the list rather than the text values from the list. I would really appreciate any help
'vin1' is a string. It is not the variable you initialized to 100. To put the variable values in the list you do this:
vin_numbers=[vin1, vin2, vin3, vin4]
You don't need need to make the variables and can put the values directly in the list.
vin_numbers=[100, 200, 300, 400]
I see. I think I was trying to do too much with it, when just making the list a list of numbers would be cleaner. thank you!
I don't understand why, but it is a common misconception by many new programmers on this forum that everything needs a variable, as in if you have 4 things you must need 4 variables. I don't know where this thinking comes from, but it is common enough that there must be some popular resource perpetuating this fallacy.

Variables are bad. Necessary, but bad. Every variable is extra code and extra potential for making mistakes. Your programs should only use as many variables as are necessary and no more. In your example you should only have 1 variable, the list of VIN numbers. Unless there is something really special about the second VIN number why should it have a variable? That can be a kind of test for deciding to use a variable. If something is "special" it probably deserves a variable, else treat it as part of a collection or ignore it.

What do I mean by "treat it as part of a collection"? In this example you have a list of VIN numbers. In the "real world" each VIN number represents something unique and special, but a program is not the real world and a VIN number is just a unique ID. Nothing special about that. Your program needs to remember VIN numbers, so there has to be some way to save them and access them and probably process them, but this will be the same for all the VIN numbers. When you have a bunch of related things like this it is best to treat them as faceless entities in a collection.

What do I mean by "ignore it"? I see a lot of Python code on this forum that makes variables for things that should be thrown away. This is particularly true in the GUI discussion group. Almost everything returned by a tkinter call should be thrown away. After I place my controls I don't need to remember the frame that holds them. Depending how I write my code I don't have to remember the handles for my controls and displays. This stuff is important when you are making things, but once you move into running things they are no longer referenced. If you are done using something, throw it away. Python is good at recycling

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass list of values to a API request URL chetansaip99 0 476 Sep-28-2021, 07:37 AM
Last Post: chetansaip99
Question Calling on a Variable using other Variables jacknewport 4 550 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  An IF statement with a List variable dedesssse 3 461 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Python dictionary with values as list to CSV Sritej26 4 917 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 559 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Create variable and list dynamically quest_ 12 1,251 Jan-26-2021, 07:14 PM
Last Post: quest_
  Giving all possible values to four different variables quest_ 7 920 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  How to discard list repeat values akanowhere 7 1,140 Dec-28-2020, 09:14 PM
Last Post: akanowhere
Question Matching variable to a list index Gilush 17 1,629 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 707 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov

Forum Jump:

User Panel Messages

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