0

Is there any way to remove double list? Right now I have a list inside a list as an output.

target_rule = [ '2.1.1 Context','2.1.2.4 Helping Us out','We know that']
target=[]
x=[s for s in target_rule if re.findall("\d",s)]

if x:  
    target.append(x)
print(target)

Output is

[['2.1.1 Context', '2.1.2.4 Helping Us out']]

But I want the output to be

['2.1.1 Context', '2.1.2.4 Helping Us out']
Abdul Wahab
  • 129
  • 1
  • 2
  • 7
  • 4
    why are you using `target` instead of just using `x`? or say, `target = x` ? – Paritosh Singh Jun 18 '19 at 21:08
  • remove `if` conditions and just `print (x)` – Transhuman Jun 18 '19 at 21:09
  • Alternately, if there's a reason you have to return this in a nested fashion, you can unnest it one level by indexing into the first element: `target[0]` – G. Anderson Jun 18 '19 at 21:12
  • You can just replace ``target.append(x)`` with ``target.extend(x)``. – AlCorreia Jun 18 '19 at 21:16
  • The simple solution is to just ```print(x)``` since x is the already a list because of the list comprehension expression ``` x=[s for s in target_rule if re.findall("\d",s)] ``` However, If you want to continuously do this and that ```target``` is an array you want to continuously add to in this fashion Well one solution would be to just iterate and append each element ``` if x: for (element in x){ target.append(element) } ``` Or you can use list append ``` target+=x ``` – Mohamed Ahmed Nabil Jun 18 '19 at 21:17

1 Answers1

1
target_rule = [ '2.1.1 Context','2.1.2.4 Helping Us out','We know that']
x=[s for s in target_rule if re.findall("\d",s)]

print(x)

x here is already a list because of the comprehension in line 3

in your code, target was redundant because you were appending a list to a list. If you would like to have a separate variable to return/print you can do

target = x
return target
static const
  • 953
  • 4
  • 16