What’s the best way to Convert a list to dict in Python2.7

nanci

I have a list like:

x = ['user=inci', 'password=1234', 'age=12', 'number=33']

I want to convert x to a dict like:

{'user': 'inci', 'password': 1234, 'age': 12, 'number': 33}

what's the quickest way?

Chris_Rands

You can do this with a simple one liner:

dict(item.split('=') for item in x)

List comprehensions (or generator expressions) are generally faster than using map with lambda and are normally considered more readable, see here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to convert a raw vector to type safe list(Arraylist)

From Dev

What's the best way to organize JPanels in a list?

From Dev

What's the best way to list size of / in Terminal?

From Dev

Best way to convert list of lists to list of tuples?

From Dev

What is the best way to append lists inside a dict?

From Dev

How to convert this tuple list to this dict in a Pythonic way?

From Dev

Convert 2 element list into dict

From Dev

Best way to aggregate a simple dict in Python

From Dev

Best way to aggregate a simple dict in Python

From Dev

Best way for dict with duplicate keys in python

From Dev

What is the best way to trim a list?

From Dev

What is the best way to trim a list?

From Dev

What is the best way to convert an array of strings into an array of objects in Angular 2?

From Java

What's the best way to convert a number to a string in JavaScript?

From Dev

What's the best way to convert an int to a char in Julia?

From Dev

What's the best way to convert pojo to JSON in Spring

From Dev

What's the best way to convert an Int to a String in Julia?

From Dev

What's the best way to convert String into [Character] in Swift?

From Dev

What's the best way to get the next 7 day names in node?

From Dev

What is the best way to search a List<List<string>>?

From Dev

What is the best way to concatenate 2 lists into a new list in Java?

From Dev

What is the best way in Python to loop over a list back to front?

From Dev

What is the best way to remove .mp3 from this list in Python?

From Dev

What is the pythonic way to this dict to list conversion?

From Dev

What is the best way to convert a list of dates so it displays in groups if it contains consecutive days?

From Dev

What's the best way to get a single random element from a List<>?

From Dev

What's the best way to declare a list of scalar values in Swift

From Dev

What's the best way to trim() all elements in a List<String>?

From Dev

What's the best way to add the same ajax function to a list of comments?

Related Related

  1. 1

    What is the best way to convert a raw vector to type safe list(Arraylist)

  2. 2

    What's the best way to organize JPanels in a list?

  3. 3

    What's the best way to list size of / in Terminal?

  4. 4

    Best way to convert list of lists to list of tuples?

  5. 5

    What is the best way to append lists inside a dict?

  6. 6

    How to convert this tuple list to this dict in a Pythonic way?

  7. 7

    Convert 2 element list into dict

  8. 8

    Best way to aggregate a simple dict in Python

  9. 9

    Best way to aggregate a simple dict in Python

  10. 10

    Best way for dict with duplicate keys in python

  11. 11

    What is the best way to trim a list?

  12. 12

    What is the best way to trim a list?

  13. 13

    What is the best way to convert an array of strings into an array of objects in Angular 2?

  14. 14

    What's the best way to convert a number to a string in JavaScript?

  15. 15

    What's the best way to convert an int to a char in Julia?

  16. 16

    What's the best way to convert pojo to JSON in Spring

  17. 17

    What's the best way to convert an Int to a String in Julia?

  18. 18

    What's the best way to convert String into [Character] in Swift?

  19. 19

    What's the best way to get the next 7 day names in node?

  20. 20

    What is the best way to search a List<List<string>>?

  21. 21

    What is the best way to concatenate 2 lists into a new list in Java?

  22. 22

    What is the best way in Python to loop over a list back to front?

  23. 23

    What is the best way to remove .mp3 from this list in Python?

  24. 24

    What is the pythonic way to this dict to list conversion?

  25. 25

    What is the best way to convert a list of dates so it displays in groups if it contains consecutive days?

  26. 26

    What's the best way to get a single random element from a List<>?

  27. 27

    What's the best way to declare a list of scalar values in Swift

  28. 28

    What's the best way to trim() all elements in a List<String>?

  29. 29

    What's the best way to add the same ajax function to a list of comments?

HotTag

Archive