'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?



Solution 1:[1]

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']}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1