2

Note: I would want j = [] to remain inside the loop Example:

x = ['Page1.pdf','Page2.pdf']
for i in x:
    s = i
    j = []
    j.append(s)
    print(j)
    

My output from print(j):

['Page1.pdf']
['Page2.pdf']

Expected Output from print(j):

['Page1.pdf','Page2.pdf']
jizhihaoSAMA
  • 11,804
  • 9
  • 23
  • 43

3 Answers3

2

You have a list of lists of strings, which you want to convert to a simple list of strings. You can do that this way:

x = [['john'],['mary']]
x = [item for sublist in x for item in sublist]

After that, x is ['john', 'mary'], so you can do what you did:

for i in x:
    print(i)
zvone
  • 16,526
  • 2
  • 40
  • 68
1
import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))

From join list of lists in python [duplicate]

noblerthanoedipus
  • 460
  • 1
  • 7
  • 22
0

You can use list comprehension in the following way:

print([i for names in x for i in names])
rafasan
  • 184
  • 8