-3

I have a jagged list in the below format,

[['x01','xxxx','yyy','01234355'],['x02','fas','gere','1434'],[],['y05','KLM'],['1231','Port','erwe'],[],['Load','Music','XF32'],['x01','JJJJ','LLJU','2342','6346CS','qras'],['x02','ASF','10'],['CAC','131','CDF$#'],['v32','rrte','dceg'],['CS223','w34','4234ye'][],
['fsdf','rwrw','cacw','fcwe'],['x01','eeee','wefw','CDE'],['x02','WEF','083DV'],[],['vrev','wef3','DV23']]

and I am expecting to group all the lists into one list which are in between the common item, in this case till the next occurance of item 'x01' should be group to one list

[

[item_01:['x01','xxxx','yyy','01234355'],['x02','fas','gere','1434'],[],['y05','KLM'],['1231','Port','erwe'],[],['Load','Music','XF32']],

[item_02:['x01','JJJJ','LLJU','2342','6346CS','qras'],['x02','ASF','10'],['CAC','131','CDF$#'],['v32','rrte','dceg'],['CS223','w34','4234ye'][],
['fsdf','rwrw','cacw','fcwe']],

[item_03:['x01','eeee','wefw','CDE'],['x02','WEF','083DV'],[],['vrev','wef3','DV23']]

]

It may be simple or repeated question, but I am new to python coding so, please help to achieve the expected format

gold_cy
  • 13,648
  • 3
  • 23
  • 45

1 Answers1

1

As pointed out in @PatrickArtner and @aws_apprentice's comments, your desired output does not exists in python: you can't have a list with a key (i.e. item_01, item_02...).
Some similar objects let you store information with keys (see Dict), but you can't have many different values for the same key.
also your input is invalid, since a comma is missing.

Anyway it is possible to understand the general idea of what you want, and I can suggest you an approach:

Create a Dictionary where the values are lists of lists:

my_list = [['x01','xxxx','yyy','01234355'],['x02','fas','gere','1434'],[],['y05','KLM'],['1231','Port','erwe'],[],['Load','Music','XF32'],['x01','JJJJ','LLJU','2342','6346CS','qras'],['x02','ASF','10'],['CAC','131','CDF$#'],['v32','rrte','dceg'],['CS223','w34','4234ye'],[],['fsdf','rwrw','cacw','fcwe'],['x01','eeee','wefw','CDE'],['x02','WEF','083DV'],[],['vrev','wef3','DV23']]


n = 0 # initialize a counter
common_item = 'x01' # identify the common item
my_dictionary = {} # create the empty dictionary
for x in my_list: # go through each element of your list
    if len(x)>0 and x[0] == common_item: # check if the first item is the common item
        n +=1 # if it is, it's a new list: increase n by 1
        identifier = 'item_{0}'.format(str(n).zfill(2)) # build the key of your dictionary with item_ + n
        my_dictionary[identifier] = [] # assign at the new key an empty list as value
    my_dictionary[identifier].append(x) # add in the list of the current key each element found in my_list

print(my_dictionary)

Output:

{'item_03': [['x01', 'eeee', 'wefw', 'CDE'], ['x02', 'WEF', '083DV'], [], ['vrev', 'wef3', 'DV23']], 'item_02': [['x01', 'JJJJ', 'LLJU', '2342', '6346CS', 'qras'], ['x02', 'ASF', '10'], ['CAC', '131', 'CDF$#'], ['v32', 'rrte', 'dceg'], ['CS223', 'w34', '4234ye'], [], ['fsdf', 'rwrw', 'cacw', 'fcwe']], 'item_01': [['x01', 'xxxx', 'yyy', '01234355'], ['x02', 'fas', 'gere', '1434'], [], ['y05', 'KLM'], ['1231', 'Port', 'erwe'], [], ['Load', 'Music', 'XF32']]}

As you can see, dictionary are not ordered: this means that item_01 could not be the first key of your dictionary.

you can also retrieve the value of a specific key:

print(my_dictionary['item_01'])

Output:

[['x01', 'xxxx', 'yyy', '01234355'], ['x02', 'fas', 'gere', '1434'], [], ['y05', 'KLM'], ['1231', 'Port', 'erwe'], [], ['Load', 'Music', 'XF32']]

And many other operations: see the official documentation, and thousand of questions in SO, starting from How to use a Python dictionary?

Gsk
  • 2,929
  • 5
  • 22
  • 29
  • Very kind of you @Gsk for the solution by understanding the intent of request, and its my bad that I referred my expected output as 'List' which should be some key value pair collection. Taking your code as reference, I tried many ways to create a list of lists instead of a dictionary but nothing worked. Can you please guide me to achieve it. – keyan'Aura Feb 13 '19 at 20:30
  • @keyan'Aura the code that I gave you transform a list of lists (`my_list`, the one that you proposed in the question) into a dictionary of list of lists (`my_dictionary`). Copy and paste my code and `print(my_dictionary)` that and see the outcome. if you want to see how dictionaries works, start with `my_dict = {'even':[2,4,6], 'odd':[1,3,5]}`. This is a dict with 2 keys (`even` and `odd`) and their values (2 lists, `[2,4,6]` and `[1,3,5]`). – Gsk Feb 13 '19 at 21:35
  • Once again Thank you @Gsk for the explanation with a simple example. – keyan'Aura Feb 13 '19 at 23:05
  • If you were able to solve your question, accept the answer. Otherwise, ask for details – Gsk Feb 13 '19 at 23:12