Python Forum
Can I use iteration to create variable names?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I use iteration to create variable names?
#9
(Oct-16-2019, 04:37 PM)Mark17 Wrote: I don't understand why. Seeing a simple example and the kind of traceback that can result might help me understand a bit more.

Simple example. Lets say we have class and want have ledger. Normal way would be to have dictionary but we can to it with separate variables:

john = 'student'
mary = 'student'
sonia = 'teacher'
How could we find out how many students we have? How could we find out names of the students? etc, etc

On the other hand, with dictonaries it's simple:

>>> class_ledger = {'john': 'student', 'maria': 'student', 'sonia': 'teacher'}
>>> len(class_ledger)                                                         # total number of people
3
>>> sum(1 for k, v in class_ledger.items() if v == 'student')                 # number of students
2
>>> print(*(k for k, v in class_ledger.items() if v == 'student'), sep='\n')  # names of students
john
maria
>>> 'maria' in class_ledger                                                   # is maria in a ledger
True
>>> class_ledger.update({'Diego': 'student'})                                 # add new person to class
>>> class_ledger
{'john': 'student', 'maria': 'student', 'sonia': 'teacher', 'Diego': 'student'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Can I use iteration to create variable names? - by perfringo - Oct-17-2019, 06:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,845 Nov-07-2023, 09:49 AM
Last Post: buran
  loop (create variable where name is dependent on another variable) brianhclo 1 1,162 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Create variable and list dynamically quest_ 12 4,475 Jan-26-2021, 07:14 PM
Last Post: quest_
  How do I get the in3.Client() to create a variable with attributes? (in3 pypi pckge) Johno 2 1,881 Jan-21-2021, 02:49 AM
Last Post: Johno
  Create new variable dependent on two existing variables JoeOpdenaker 6 3,099 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Alternative to dynamic variable names catosp 13 4,637 Jun-20-2020, 03:45 PM
Last Post: catosp
  Changing a variable's name on each iteration of a loop rix 6 85,012 Jan-03-2020, 07:06 AM
Last Post: perfringo
  How do I create a Dynamic Variable? Nwb 1 2,767 Jun-10-2018, 11:50 AM
Last Post: volcano63
  Is it possible to create a variable with the name of an output of another variable Panda 8 4,624 Jun-06-2018, 09:14 PM
Last Post: Panda
  2D Array/List OR using variables in other variable names? IAMK 4 3,895 Apr-16-2018, 09:09 PM
Last Post: IAMK

Forum Jump:

User Panel Messages

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