Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign a list that is set as a variable to another list (python)

In building a config file, I’m trying to add a list to another list. I’d rather not use any functions like append or python logic in this config file. Some examples are listed below:

config = {
        'users': [
                'user1',
                'user2',
                'user3'
        ]
}

admin_access = {
        'allowed_users': [
                config['users'],
                'adminuser1',
                'adminuser2'
        ]
}

Am I going about this the right way or am I completely off?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think what you may be looking for is:

admin_access = {
        'allowed_users': [
                *config['users'],
                'adminuser1',
                'adminuser2'
        ]
}

Which gives:

{'allowed_users': ['user1', 'user2', 'user3', 'adminuser1', 'adminuser2']}

If you couldn’t directly create admin_access like this, you could also add on the wanted list like this:

# Given
config = {'users': ['user1', 'user2', 'user3']}
admin_access = {'allowed_users': ['adminuser1', 'adminuser2']}

# Do
admin_access['allowed_users'] += config['users']

# Outputs
print(admin_access)
{'allowed_users': ['adminuser1', 'adminuser2', 'user1', 'user2', 'user3']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading