Python | Unpacking dictionary keys into tuple

Python | Unpacking dictionary keys into tuple

In Python, if you have a dictionary and you want to unpack its keys into a tuple, you can easily do that by converting the dictionary keys view to a tuple.

Here's an example:

# Sample dictionary data = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 } # Unpack dictionary keys into a tuple keys_tuple = tuple(data.keys()) print(keys_tuple) # Output: ('a', 'b', 'c', 'd') 

In the code above, data.keys() returns a view of the dictionary's keys. By passing it to the tuple constructor, you can convert this view into a tuple containing all the dictionary's keys.


More Tags

documentation-generation http2 terraform location-provider datetime home-directory intentservice intentfilter keyword-argument matrix

More Programming Guides

Other Guides

More Programming Examples