2

I want to convert this list of list to list of strings here. The idea behind converting is to remove the duplicates from the output.

Here is the original list: (The output from json.loads(file.json))

I have further simplified the dictionary output and got here a list of keys.

> [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg',
> u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]

Now the list of keys contain another list inside it in some keys and duplicates as well.

I tried to convert this list of lists to a list of strings using something like

> [','.join(x) for x in list01]

But that did not work for me as i also got some single characters in the output as well like ['xyza'. 'x', 'y',...]

How will I be able to parse the list data here to get an output something like

['xyza', 'xyzb', 'xyzc',...,'xyzx']
Salvatore
  • 10,815
  • 4
  • 31
  • 69
noob_coder
  • 749
  • 4
  • 15
  • 35

5 Answers5

1

since you have a list of list and string in it and you want list of string with no duplicate present.

create a resultant list, and then iterate through the parent list and check the type of each object, if it of type list then iterate through the sublist and check that string in sublist is present in the resultant list or not, if present then ignore else if not present then add it to list, same goes for the parent list iterate when it is not of type list.

res = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]

result =[]
for i in res:
    if isinstance(i, list):
        for j in i:
            if j not in result:
                result.append(j)
    else:
        if i not in result:
            result.append(i)

print(result)

output:

['xyza', 'xyzb', 'xyzc', 'xyzd', 'xyze', 'xyzf', 'xyzg', 'xyzh','xyxv']

if you want to make it little faster, then instead of result as list , you can create it as a dictionary and without checking the condtition if string already present or not, you just update the dictionary and once you iterate through whole list, convert the dictionary keys to list and that is your answer.

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

You can try this:

mylist = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'],` u'xyzd', [u'xyza'],[u'xyxv']]
    
new_list = []
for i in mylist:
    if isinstance(i, list):
        for j in i:
            new_list.append(j)
    else:
        new_list.append(i)

remove duplicates:

new_list = list(set(new_list))

output:

['xyzc', 'xyzf', 'xyze', 'xyzg', 'xyxv', 'xyza', 'xyzh', 'xyzb', 'xyzd']
Jonas
  • 1,749
  • 1
  • 10
  • 18
1

Not all items are lists itself so this should be checked. Also, turn to a set and back to a list for unique values:

l = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]

new_list = []
for sublist in l:
    if isinstance(sublist, list):
        for item in sublist:
            new_list.append(item)
    else:
        new_list.append(sublist)

new_list = list(set(new_list))

>>> new_list
['xyzc', 'xyza', 'xyzd', 'xyzb', 'xyzh', 'xyxv', 'xyzg', 'xyzf', 'xyze']
BramAppel
  • 1,346
  • 1
  • 9
  • 21
0

Let's call your list of lists the variable values

def to_str_list(values):
    vars = list()
    for x in values:
      while type(x[0]) == list:
        x = x[0]
      for s in x:
         if s not in vars:         
             vars.append(s) 
    return vars
  
0
[d['value'] for d in l] 

If value might be missing, you can use

[d['value'] for d in l if 'value' in d] 
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39