-3

I have a dictionary which looks like this:

{"bbox": [[1386, 111], [1475, 111], [1475, 159], [1386, 159]]}

How could i get rid of square brackets of value in it, so it looks like this:

{"bbox": [1386, 111, 1475, 111, 1475, 159, 1386, 159]}

How could I write a function which would do that?

d = {k:v[0] for k,v in d.items()}

  • 1
    Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) Also, it is a nested list. not a dictionary. – Massoud Mar 06 '23 at 05:05
  • That isn't a dictionary. Printing it without brackets seems like a complete waste of time. The list is still going to be multidimensional, regardless of how much you destroy the printed format. – OneMadGypsy Mar 06 '23 at 05:06

1 Answers1

0
flat_list = [item for sublist in l for item in sublist]

which is

flat_list = []
for sublist in l:
    for item in sublist:
        flat_list.append(item)

function:

def flatten(l):
    return [item for sublist in l for item in sublist]
Massoud
  • 361
  • 1
  • 2
  • 16