Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
Python - Create list out of all variables in same .py file

So I have this code below of categories and I will sometimes update it by adding a new category, then I have to manually add that category to the list at the bottom INITIAL_GOAL_CATEGORIES it'd be much easier if this list was automatically updated whenever I create a new dict variable. Is there a way to do this? I export the INITIAL_GOAL_CATEGORIES variable and use it elsewhere so if I can set that variable name to a list of all other variables that'd be great. This file will only contain dicts of categories and the list of all of them at the bottom.

categories.py

# Categories
ART = dict(name='Art', emoji='🎨')
CULINARY = dict(name='Culinary', emoji='🍳')
DIET = dict(name='Diet', emoji='πŸ₯—')
DIY = dict(name='DIY', emoji='πŸ”¨')
FINANCE = dict(name='Finance', emoji='πŸ’°')
FITNESS = dict(name='Fitness', emoji='πŸ‘Ÿ')
GAMING = dict(name='Gaming', emoji='πŸ•ΉοΈ')
MARTIAL_ARTS = dict(name='Martial Arts', emoji='πŸ₯‹')
MUSIC = dict(name='Music', emoji='🎡')
RELATIONSHIP = dict(name='Relationship', emoji='πŸ’—')
SELF_CARE = dict(name='Self-Care', emoji='πŸ€—')
SELF_IMPROVEMENT = dict(name='Self-Improvement', emoji='✨')
STREAMING = dict(name='Streaming', emoji='⏺️')
TRAVEL = dict(name='Travel', emoji='✈️')
WEIGHT_TRAINING = dict(name='Weight Training', emoji='πŸ‹οΈ')

INITIAL_GOAL_CATEGORIES = [ART, CULINARY, DIET, DIY, FITNESS, GAMING, MARTIAL_ARTS, MUSIC,
                           RELATIONSHIP, SELF_IMPROVEMENT, SELF_CARE, STREAMING, TRAVEL,
                           WEIGHT_TRAINING]
over 4 years ago Β· Santiago Trujillo
1 answers
Answer question

0

If you want to create a list that update itself when you add this kind of global values, here what you need:

# Categories
ART = dict(name='Art', emoji='🎨')
CULINARY = dict(name='Culinary', emoji='🍳')
DIET = dict(name='Diet', emoji='πŸ₯—')
DIY = dict(name='DIY', emoji='πŸ”¨')
FINANCE = dict(name='Finance', emoji='πŸ’°')
FITNESS = dict(name='Fitness', emoji='πŸ‘Ÿ')
GAMING = dict(name='Gaming', emoji='πŸ•ΉοΈ')
MARTIAL_ARTS = dict(name='Martial Arts', emoji='πŸ₯‹')
MUSIC = dict(name='Music', emoji='🎡')
RELATIONSHIP = dict(name='Relationship', emoji='πŸ’—')
SELF_CARE = dict(name='Self-Care', emoji='πŸ€—')
SELF_IMPROVEMENT = dict(name='Self-Improvement', emoji='✨')
STREAMING = dict(name='Streaming', emoji='⏺️')
TRAVEL = dict(name='Travel', emoji='✈️')
WEIGHT_TRAINING = dict(name='Weight Training', emoji='πŸ‹οΈ')

INITIAL_GOAL_CATEGORIES = [value for name, value in globals().items() if
                           name.isupper()]

This go through all values in globals() and check if the name of the value is upper case. If it met the criteria, it is a category and we add it to the categories list.

But as said in comments: a dictionary is better:

# Categories
INITIAL_GOAL_CATEGORIES = {
    'Art': {'emoji': '🎨'},
    'Culinary': {'emoji': '🍳'},
    'Diet': {'emoji': 'πŸ₯—'},
    'DIY': {'emoji': 'πŸ”¨'},
    'Finance': {'emoji': 'πŸ’°'},
    'Fitness': {'emoji': 'πŸ‘Ÿ'},
    'Gaming': {'emoji': 'πŸ•ΉοΈ'},
    'Martial Arts': {'emoji': 'πŸ₯‹'},
    'Music': {'emoji': '🎡'},
    'Relationship': {'emoji': 'πŸ’—'},
    'Self-Care': {'emoji': 'πŸ€—'},
    'Self-Improvement': {'emoji': '✨'},
    'Streaming': {'emoji': '⏺️'},
    'Travel': {'emoji': '✈️'},
    'Weight Training': {'emoji': 'πŸ‹οΈ'},
}
over 4 years ago Β· Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
Β© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!