arshpreet
arshpreet

Reputation: 729

Save list in Python

I want to save a list of strings in the python Array. My code is as below:

import os
Images_list=[os.listdir("/home/metal-machine/Pictures/new")]
print Images_list[0]

It only prints the list in the [0] and when I use

print Images_list[1]

It returns the error as: IndexError: list index out of range

I am sure this "index out of range" is because all the elements of list are not separated by comma.

For that I can also use split() method in Python.

images_list.split()

And the above command splits the files but putting commas in-between all of them, but why Python By-default not put commas in between all the elements? Is sunch kind of list is valid in Python?

Upvotes: 0

Views: 775

Answers (1)

TheSilentProwler
TheSilentProwler

Reputation: 73

Why not write code that is a bit more error free

for images in Images_list:
    print images

Upvotes: 1

Related Questions