Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting a dictionary in python into keys and values

How can I take a dictionary and split it into two lists, one of keys, one of values. For example take:

{'name': 'Han Solo', 'firstname': 'Han', 'lastname': 'Solo', 'age': 37, 'score': 100, 'yrclass': 10}

and split it into:

['name', 'firstname', 'lastname', 'age', 'score', 'yrclass']
# and
['Han Solo', 'Han', 'Solo', 36, 100, 10]

Any ideas guys?

like image 464
Fergus Barker Avatar asked Oct 25 '10 23:10

Fergus Barker


People also ask

How do you split a dictionary value in Python?

Method 1: Split dictionary keys and values using inbuilt functions. Here, we will use the inbuilt function of Python that is . keys() function in Python, and . values() function in Python to get the keys and values into separate lists.

How do you split a dictionary by key in Python?

Use data. viewkeys() if you are using Python 2 still. Dictionary views give you a set-like object, on which you can then use set operations; & gives you the intersection.

How do you separate a key from a value in a dictionary?

Creating a Dictionary To do that you separate the key-value pairs by a colon(“:”). The keys would need to be of an immutable type, i.e., data-types for which the keys cannot be changed at runtime such as int, string, tuple, etc. The values can be of any type.

How do you create a dictionary using keys and values?

To create a Python dictionary, we pass a sequence of items (entries) inside curly braces {} and separate them using a comma ( , ). Each entry consists of a key and a value, also known as a key-value pair. Note: The values can belong to any data type and they can repeat, but the keys must remain unique.


2 Answers

Not that hard, try help(dict) in a console for more info :)

keys = dictionary.keys()
values = dictionary.values()

For both keys and values:

items = dictionary.items()

Which can be used to split them as well:

keys, values = zip(*dictionary.items())

Note 0 The order of all of these is consistent within the same dictionary instance. The order of dictionaries in Python versions below 3.6 is arbitrary but constant for an instance. Since Python 3.6 the order depends on the insertion order.

Note 1 In Python 2 these all return a list() of results. For Python 3 you need to manually convert them if needed: list(dictionary.keys())

like image 70
Wolph Avatar answered Oct 17 '22 18:10

Wolph


as a compliment to @Wolph answer,

its important to say that using zip may be much slower!


Added split_with_tuple - as split returns view objects as of python3

In [1]: def split(d):
   ...:     return d.keys(), d.values()
   ...:
   ...:

In [2]: def split_with_tuple(d):
   ...:     return tuple(d.keys()), tuple(d.values())
   ...:
   ...:

In [3]: def split_with_zip(d):
   ...:     return zip(*d.items())
   ...:
   ...:

In [4]: d = {i:str(i) for i in range(10000)}

In [5]: %timeit split(d)
265 ns ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [6]: %timeit split_with_tuple(d)
151 µs ± 772 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [7]: %timeit split_with_zip(d)
950 µs ± 15.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
like image 43
Alonme Avatar answered Oct 17 '22 17:10

Alonme