-4

I want to change a dictionary below ...

dict = {
    'A': [('B', 1), ('C', 3), ('D', 7)],
    'B': [('D', 5)],
    'C': [('D', 12)] }

into other form like this:

dict = [
('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7),
('B', 'D', 5), ('C', 'D', 12)]

This is what I done.

dict = {
 'A': [('B', 1), ('C', 3), ('D', 7)],
 'B': [('D', 5)],
 'C': [('D', 12)] }

if(i[0] in dict):
    value = dict[i[0]]
    newvalue = i[1],i[2]
    value.append(newvalue)
    dict1[i[0]]=value

else:
    newvalue = i[1],i[2]
    l=[]
    l.append(newvalue)
    dict[i[0]]=l   

print(dict)

Thanks

Sbitn
  • 1
  • 1
  • Did you have a question or problem doing this? – Sayse Jun 19 '20 at 15:00
  • Can you confirm that `('B', 'C', 12)` is in the expected output? I think what you want might be `('B', 'D', 5)` instead. And also, please post what you have tried so far so that people can go from there to provide help. – Jake Tae Jun 19 '20 at 15:02
  • 1
    Please confirm if expected output is correct and also Please share if you have written some code to do transformation – Shivam Seth Jun 19 '20 at 15:05

4 Answers4

1

Python tuple is an immutable object. Hence any operation that tries to modify it (like append) is not allowed. However, following workaround can be used.

dict = {
    'A': [('B', 1), ('C', 3), ('D', 7)],
    'B': [('D', 5)],
    'C': [('D', 12)] }

new_dict = []

for key, tuple_list in dict.items():
    for tuple_item in tuple_list:
        entry = list(tuple_item)
        entry.append(key)
        new_dict.append(tuple(entry))

print(new_dict)

Output: [('B', 1, 'A'), ('C', 3, 'A'), ('D', 7, 'A'), ('D', 5, 'B'), ('D', 12, 'C')]

RonZhang724
  • 535
  • 8
  • 17
0

You can iterate through the dictionary using .items(). Notice that each value is by itself a list of tuples. We want to unpack each tuple, so we need a nested for-loop as shown below. res is the output list that we will populate within the loop.

res = []
for key, values in dict.items():
    for value in values:
        res.append((key, value[0], value[1]))

Sample output:

>>> res
[('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7), ('B', 'D', 5), ('C', 'D', 12)]

EDIT: If value is a tuple of more than two elements, we would modify the last line as follows, using tuple unpacking:

res.append((key, *value))

This effectively unpacks all the elements of value. For example,

>>> test = (1, 2, 3)
>>> (0, *test)
(0, 1, 2, 3)
Jake Tae
  • 1,681
  • 1
  • 8
  • 11
  • What if the tuple have more than two elements? – RonZhang724 Jun 19 '20 at 15:06
  • Then we wouldn't be able to use `res.append((key, value[0], value[1]))`. Instead, I would use a different mode of tuple unpacking, like `res.append((key, *value))`. However, given the sample input and output of the OP, I think the implementation above would suffice. – Jake Tae Jun 20 '20 at 18:21
0

A simple aproach could be

new_dict = []
for letter1, list in dict.items():
    for letter2, value in list:
        new_dict.append([letter1, letter2, value])
0

With list comprehension;

dict_ = {
    'A': [('B', 1), ('C', 3), ('D', 7)],
    'B': [('D', 5)],
    'C': [('D', 12)] }

result = [(key, value[0], value[1]) for key, list_ in dict_.items() for value in list_]

Output;

[('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7), ('B', 'D', 5), ('C', 'D', 12)]
Sy Ker
  • 2,047
  • 1
  • 4
  • 20