The order of the resulted list is irrelevant, I need to add the elements afterwards.
JavaScript
x
9
1
Input1: [[{'a': 57}], 87]
2
Expect Output1: [57,87]
3
4
Input2: [[{'a': 57, 'b':12}], 87]
5
Expect Output2: [57,12,87]
6
7
Input3: [[{'a': 57}], 12, [{'a':34}], 87]
8
Expect Output3: [57,12,34,87]
9
Advertisement
Answer
That’s Pretty Simple all you Need is to just check the Type of the object
Then If it’s a list get the first index of it which is a dict
So for example if i have
[{1,2,3}]
It will convert it to Just {1,2,3}
Then Looping Through The values(not the keys)
in the Dict
and just adds it to a list
Whereas If it’s not a list then just add it to the list
JavaScript
1
12
12
1
Input3 = [[{'a': 57}], 12, [{'a':34}], 87]
2
3
li = []
4
5
for i in Input3:
6
if type(i) == list:
7
for item in i[0].values():
8
li.append(item)
9
10
else:
11
li.append(i)
12