-1

I have created a dictionary and filled it with dictionaries by using two for-each loops.

 y = {'Name': {'User0': 'Alicia', 'User1': 'Lea', 'User2': 'Jan', 'User3': 'Kot', 'User4': 'Jarvis'}, 
      'Password': {'User0': 'kokos', 'User1': 'blbec ', 'User2': 'morous', 'User3': 'mnaumnau', 'User4': 'Trav3_liK'}}

Each user has a name and a password. It would be much easier to set my data like this:

y = {UserX : ["Name", "Password"], ... }

Is there any way I can "unify" my previous code?

dot.Py
  • 5,007
  • 5
  • 31
  • 52
Johnny
  • 7
  • 1

1 Answers1

3

Supposing User and Password dicts have the same users:

y = {
    user_id: (name, y['Password'][user_id])
    for user_id, name in y['Name'].items()
}
Javier
  • 2,752
  • 15
  • 30
  • to be compliant to user request, build a list not a tuple. But that's the spirit. – Jean-François Fabre Apr 14 '17 at 11:46
  • A tuple is the proper type in this case. Jonny, When you have different things, use a tuple instead of a list. A list is for many of the same. In this case, a user name and a user password are semantically different things even though they are both str. – Javier Apr 14 '17 at 11:49
  • I agree with that. Tuple is the way if you don't plan to add more items. That should be an edit to your answer, not a comment. – Jean-François Fabre Apr 14 '17 at 11:51
  • Yes, tuples are a sort of anonymous record of sorts. Namedtuples are an improvement: https://docs.python.org/dev/library/collections.html#collections.namedtuple – Javier Apr 14 '17 at 11:51