Split dictionary values into two separate values

Question:

I have the following dictionary which I want to split the lists into two distinct lists with different keys.

{
   "industry": [
      [
         "IT Services and IT Consulting",
         "New York City, NY"
      ],
      [
         "IT Services and IT Consulting",
         "La Jolla, California"
      ],
      [
         "Software Development",
         "Atlanta, GA"
      ]
   ]
}

I am expecting to have as output:

{
   "industry": [
      "IT Services and IT Consulting",
      "IT Services and IT Consulting",
      "Software Development"
   ],
   "country": [
      "New York City, NY",
      "La Jolla, California",
      "Atlanta, GA"
   ]
}
Asked By: Joe

||

Answers:

d = {
    "industry": [
      [
         "IT Services and IT Consulting",
         "New York City, NY"
      ],
      [
         "IT Services and IT Consulting",
         "La Jolla, California"
      ],
      [
         "Software Development",
         "Atlanta, GA"
      ]
    ]
}

res = {
    'industry': [t[0] for t in d['industry']],
    'country': [t[1] for t in d['industry']]
}
print(res)

prints

{'industry': ['IT Services and IT Consulting',
  'IT Services and IT Consulting',
  'Software Development'],
 'country': ['New York City, NY', 'La Jolla, California', 'Atlanta, GA']}

Alternative solution:

industry, country = list(zip(*d['industry']))
res = {'industry': industry, 'country': country}

Explanation: Given your dictionary d, create a new dictionary res by iterating over each tuple t = (industry, country) in the list d["industry"] and using the first element t[0] = industry for the "industry" list in res and the second element t[1] = country for the "country" list in res via a list comprehension ([_ for _ in _]).

Answered By: Michael Hodel

Here is a solution using zip to transpose the data, then to combine it to the header to form an new dictionary:

d = {'industry': [['IT Services and IT Consulting', 'New York City, NY'],
                  ['IT Services and IT Consulting', 'La Jolla, California'],
                  ['Software Development', 'Atlanta, GA']]
    }


out = dict(zip(['industry', 'country'], map(list, zip(*d['industry']))))

output:

{'industry': ['IT Services and IT Consulting', 
              'IT Services and IT Consulting',
              'Software Development'],
 'country': ['New York City, NY',
             'La Jolla, California',
             'Atlanta, GA']}
Answered By: mozway

Alternative solution with pandas:

import pandas as pd

df = pd.DataFrame(your_dict)
df.industry.agg(pd.Series).rename(columns={0:'industry', 1:'country'}).to_dict()
Answered By: Nuri Taş

Using python defaultdict and a single loop,

from collections import defaultdict
d = {"industry":[["IT Services and IT Consulting","New York City, NY"],["IT Services and IT Consulting","La Jolla, California"],["Software Development","Atlanta, GA"]]}
result = defaultdict(list)
for i in d["industry"]:
    result["industry"].append(i[0])
    result["country"].append(i[1])
print(dict(result))

Working Code: https://rextester.com/AGAW59163

Answered By: Always Sunny
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.