0

I have a list with four lists in it. I want to assign each inner list a name, basically convert the list with four lists into a dictionary with four named lists. How can I do that in a Pythonic way? I saw this question but it was too specfic.

dictionary["list1"] = lists[0]
dictionary["list2"] = lists[1]
dictionary["list3"] = lists[2]
dictionary["list4"] = lists[3]

Edit: I would like to know how to do that with arbitrary dictionary keys - not just list1,list2,list3, etc.

5
  • Have you tried to adapt that answer to work for you? Your problem is quite similar to that one Commented Mar 14, 2016 at 18:06
  • It was too dense for me to understand, and I thought generalizing the problem a bit would get me a simple answer. Commented Mar 14, 2016 at 18:11
  • Then you are not going to understand the solution I posted. You might want to post something that you've attempted, so that we can show you where you went wrong Commented Mar 14, 2016 at 18:12
  • Why the negativity? I understood it in a few minutes :) Commented Mar 14, 2016 at 18:22
  • My solution is no more complex than the solution on the post you linked. I wasn't being negative - simply stating that you have two approximately equally complex solutions to two very similar problems Commented Mar 14, 2016 at 18:33

1 Answer 1

1

This should do the trick

import itertools

names = "mains_voltage mains_time CT_voltage CT_time".split()
answer = {name:L for name,L in itertools.izip(names, lists)}
Sign up to request clarification or add additional context in comments.

6 Comments

I would like to know how to do that with arbitrary dictionary keys - not just list1,list2,list3, etc
what would be a names source? please clarify.
The dictionary will be initialized with keys, but empty values
@Rogalski: names source? What do you mean?
@M.Hassaan: it will have empty values only if lists contains empty lists
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.