How do I concisely convert a python dictionary to a list of tuples, given a dictionary in which the values may be different types?

John

Important: If the value is a list, then I want to get multiple key, value tuples for that key and each item in the list. The values in the dictionary will be of multiple types, including at least strings, ints, and lists.

I have a solution, below, but I'm wondering if there's a more concise way to do this.

# Sample data
d = {'key1': 'string1', 
    'key2': 15, 
    'key3': ['item1', 'item2', 'item3', 'item4']}

# This code does what I want
parameters = []
for k, v in d.iteritems():
    if isinstance(v, str):
        parameters.append((k ,v))
    elif isinstance(v, int):
        parameters.append((k, v))
    elif isinstance(v, list):
        for x,y in zip(itertools.repeat(k), v):
            parameters.append((x, y))
    else:
        continue

parameters
# [('key1', 'string1'),
#  ('key2', 15),
#  ('key3', 'item1'),
#  ('key3', 'item2'),
#  ('key3', 'item3'),
#  ('key3', 'item4')]
AKS

Here is a shorter version with list comprehension:

>>> d = {'key1': 'string1', 'key2': 15, 'key3': ['item1', 'item2', 'item3', 'item4']}
>>> parameters = [(k, v1) for k, v in d.iteritems() for v1 in (v if isinstance(v, list) else [v])]
>>> parameters
[('key3', 'item1'), ('key3', 'item2'), ('key3', 'item3'), ('key3', 'item4'), ('key2', 15), ('key1', 'string1')]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert Python dictionary which has list as values into simple dictionnary

How can I convert a dictionary into a list of tuples?

How to convert a list of tuples to a dictionary of dictionaries in Python?

In Python, given a dictionary with lists in the values, how do I sort the dictionary based on the amount of items in that list?

How to convert String values in a dictionary to a list in python?

How do I convert a list of tuples to a dictionary

How do I convert a list/dictionary into a Dataframe?

Convert a list of tuples to a dictionary

How to convert a dictionary to a list of keys, with repeat counts given by the values?

How do I convert a string to a dictionary which appears to already be a dictionary?

Convert list of tuples of several values into dictionary in Python 3.8

Convert a list of tuples to a dictionary, based on tuple values

How do I convert a list of lists to a python dictionary of the following format?

How to convert dictionary inside a dictionary which contains a list of tuples into a pandas dataframe

How to convert this dictionary into a list of tuples?

How do i sort a dictionary by value that has tuples as values

How do I group two items in a list of tuples as a dictionary?

How to convert a list of 2-tuples into dictionary keys in Python?

How to convert a dictionary into a list of tuples without dict functions? [PYTHON]

How do I convert a list with values of different lengths into a dictionary?

How do I join a list of values into a Python dictionary?

How do I convert a list to dictionary with specific format in python?

Convert Dictionary to a list of Tuples

How do I convert list into dictionary in Python?

Convert tuples to list of dictionary

python convert list of tuples to composite key dictionary

How to create a dictionary given values and keys as tuples of tuples

Convert a list of tuples into a dictionary and adding count information as dictionary values

How do I remove string '' from python dictionary list values?