1

this is what the print function gives me

/home/codes/paths/0000001090.png
/home/codes/paths/0000001091.png
/home/codes/paths/0000001092.png
/home/codes/paths/0000001093.png

I want to have it like

[/home/codes/paths/0000001090.png
/home/codes/paths/0000001091.png
/home/codes/paths/0000001092.png
/home/codes/paths/0000001093.png]

so I tried use list.append

raw_image_path= image_path+str(image_id).zfill(10)+'.png'
all_images = []   
all_images.append(raw_image_path)
print (all_images)

but it seems to append every printed value separately

    [/home/codes/paths/0000001090.png]
    [/home/codes/paths/0000001091.png]
    [/home/codes/paths/0000001092.png]
    [/home/codes/paths/0000001093.png]

how can I solve this ?

Anna Cury
  • 11
  • 1
  • 3
  • 1
    to print something without going to next line do `print "[",` with trailing comma to continue printing on same line (in python 3 you'd do `print("[", end="")`) – Tadhg McDonald-Jensen Mar 11 '17 at 20:12
  • 1
    will refer to [this question](http://stackoverflow.com/questions/5598181/python-print-on-same-line) as closely related and will likely solve your issue. – Tadhg McDonald-Jensen Mar 11 '17 at 20:14

1 Answers1

3

You seem to have a loop and are creating and printing a new list every single iteration.

for ...:
    raw_image_path= image_path+str(image_id).zfill(10)+'.png'
    all_images = []   # new list
    all_images.append(raw_image_path)
    print (all_images) # printing a single list
# 'len(all_images) == 1' here

Options:

1) Move all_images outside, before the loop, and append inside, then print outside, after

2) Try list comprehension instead.

In [1]: all_images = [ "{}{}.png".format('/', str(image_id).zfill(10)) for image_id in range(1090, 1095) ]

In [2]: all_images
Out[2]:
['/0000001090.png',
 '/0000001091.png',
 '/0000001092.png',
 '/0000001093.png',
 '/0000001094.png']
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245