Patrick Singer
Patrick Singer

Reputation: 1

How to remove extraneous square brackets from a nested list inside a dictionary?

I have been working on a problem which involves sorting a large data set of shop orders, extracting shop and user information based on some parameters. Mostly this has involved creating dictionaries by iterating through a data set with a for loop and appending a new list, like this:

sshop = defaultdict(list)
for i in range(df_subset.shape[0]):
    orderid, sid, userid, time = df.iloc[i]
    sshop[sid].append(userid)
sData = dict(sshop)


#CREATES DICTIONARY OF UNIQUE SHOPS WITH USER DATA AS THE VALUE
shops = df_subset['shopid'].unique()
shops_dict = defaultdict(list)
for shop in shops: 
    shops_dict[shop].append(sData[shop])
shops_dict = dict(shops_dict)

shops_dict looks like this at this point:

{10009: [[196962305]], 10051: [[2854032, 48600461]], 10061: [[168750452, 194819216, 130633421, 
 62464559]]}

To get to the final stages I have had to repeat lines of code similar to these a couple of times. What seems to happen everytime I do this is that the VALUES in the dictionaries gain a set of square brackets.

This is one of my final dictionaries:

{10159: [[[1577562540.0, 1577736960.0, 1577737080.0]], [[1577651880.0, 1577652000.0, 1577652960.0]]], 
 10208: [[[1577651040.0, 1577651580.0, 1577797080.0]]]}

I don't entirely understand why this is happening, asides from I believe it is something to do with using defaultdict(list) and then converting that into a dictionary with dict(). These extra brackets, asides from being a little confusing, appear to be causing some problems for accessing the data using certain functions. I understand that there needs to be two sets of square brackets in total, one set that encases all the values in the dictionary key and another inside of that for each of the specific sets of values within that key.

My first question would be, is it possible to remove a specific set of square brackets from a dictionary like that? My second question would be, if not - is there a better way of creating new dictionaries out the data from an older one without using defaultdict(list) and having all those extra square brackets?

Any help much appreciated! Thanks :)!

Upvotes: 0

Views: 1373

Answers (1)

Md Zafar Hassan
Md Zafar Hassan

Reputation: 283

In second loop use extend instead of append.

for shop in shops: 
    shops_dict[shop].extend(sData[shop])
shops_dict = dict(shops_dict)

Upvotes: 2

Related Questions