In Python I have a group of lists that track information about some users:
user_id = [1,2,3,4,5]
user_name = ['bob', 'alice', 'jerry', 'lisa', 'tom']
user_email = ['bob@email.com', 'alice@email.com', 'jerry@email.com', 'lisa@email.com', 'tom@email.com']
...
where the i'th element in each list correspond to each other.
I want to get user info "x" given info "y". In most cases I'd use a dictionary for this for the constant lookup time, but I don't want to build and maintain dozens of dictionaries.
If I maintain a dictionary for every pair of lists shown above I'd have
name:email
email:name
name:id
id:name
email:id
id:email
which already starts getting unmanageable - and grows very quickly with the number of attributes.
I could have everything map to user_id, and then have only 2n dictionaries, but happy to learn of a more appropriate data structure for this use case.
To illustrate how the code is currently implemented:
def get_email_by_user_id(user_id):
return [email for email, uid in zip(user_email, user_id) if uid == user_id][0]
As you can imagine, very slow :P