Python | Split dictionary keys and values into separate lists

Python | Split dictionary keys and values into separate lists

To split the keys and values of a dictionary into separate lists, you can use the keys() and values() methods of the dictionary and then convert the results into lists. Here's how you can do it:

data = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 } keys = list(data.keys()) values = list(data.values()) print("Keys:", keys) # ['a', 'b', 'c', 'd'] print("Values:", values) # [1, 2, 3, 4] 

The keys() method returns a view object that displays a list of all the keys, and the values() method returns a view object that displays a list of all the values in the dictionary. By wrapping them with the list() function, you convert these view objects into lists.


More Tags

css-paged-media databricks tempus-dominus-datetimepicker .net-6.0 indexof swift3 google-cloud-platform core-image playsound orbital-mechanics

More Programming Guides

Other Guides

More Programming Examples