471,337 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

interating over single element array

Hi all,

Can someone please explain to me why I can't do something like this:

a = 1

for value in a:
print str(value)

If I run this I get the error:

'int' object is not iterable

Obivously this is an absurd example that I would never do, but in my
application the length of 'a' can be anything greater than 0, and I want to
be able to handle cases when 'a' has only one element without coding a
special case just in the event that len(a) = 1.

any suggestions are appreciated,
trevis
Jun 8 '07 #1
8 1321
In <4B******************@newssvr21.news.prodigy.net >, T. Crane wrote:
Can someone please explain to me why I can't do something like this:

a = 1

for value in a:
print str(value)

If I run this I get the error:

'int' object is not iterable
Well the message explains why you can't do this. `a` is bound to an
integer and integers are not iterable.
Obivously this is an absurd example that I would never do, but in my
application the length of 'a' can be anything greater than 0, and I want to
be able to handle cases when 'a' has only one element without coding a
special case just in the event that len(a) = 1.
``len(a)`` wouldn't work either because integers have no "length":

In [16]: a = 1

In [17]: len(a)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most recent call last)

/home/new/<ipython console>

TypeError: len() of unsized object
any suggestions are appreciated,
Yes, don't try iterating over objects that are not iterable. ;-)

What you *can* do is iterating over lists, tuples or other iterables with
just one element in them. Try ``a = [1]``.

Ciao,
Marc 'BlackJack' Rintsch
Jun 8 '07 #2
>
>any suggestions are appreciated,

Yes, don't try iterating over objects that are not iterable. ;-)
Ah, yes... I hadn't thought of that :)
thanks,
trevis
>
What you *can* do is iterating over lists, tuples or other iterables with
just one element in them. Try ``a = [1]``.

Ciao,
Marc 'BlackJack' Rintsch

Jun 8 '07 #3
On Jun 8, 11:54 am, "T. Crane" <tcr...@REMOVETHISuiuc.eduwrote:
any suggestions are appreciated,
Yes, don't try iterating over objects that are not iterable. ;-)

Ah, yes... I hadn't thought of that :)

thanks,
trevis
What you *can* do is iterating over lists, tuples or other iterables with
just one element in them. Try ``a = [1]``.
Ciao,
Marc 'BlackJack' Rintsch

You can also do this (if tuples are okay in your case):

a = 1,

The comma turns 'a' into a tuple (1,) which is both iterable and has a
length of 1.

I have run into this issue before with a function that took a list of
filenames (strings), and needed to iterate over the list to operate on
the input files. For the case when the input would be a single file, I
needed to turn the input string into an iterable such that the 'for'
loop would not iterate on the filename characters (a rather
undesirable gotcha, you understand :-) ). So I solved my problem like
this:

def loadfiles(filelist):
if not isinstance(filelist, list):
filelist = filelist,
for filename in filelist:
f = open(filename,'r')
#do interesting stuff with file, etc...

...and it's been working very well.

Cheers,
-Basilisk96

Jun 8 '07 #4

"Basilisk96" <ba********@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
| On Jun 8, 11:54 am, "T. Crane" <tcr...@REMOVETHISuiuc.eduwrote:
| You can also do this (if tuples are okay in your case):
|
| a = 1,
|
| The comma turns 'a' into a tuple (1,) which is both iterable and has a
| length of 1.
|
| I have run into this issue before with a function that took a list of
| filenames (strings), and needed to iterate over the list to operate on
| the input files. For the case when the input would be a single file, I
| needed to turn the input string into an iterable such that the 'for'
| loop would not iterate on the filename characters (a rather
| undesirable gotcha, you understand :-) ). So I solved my problem like
| this:
|
| def loadfiles(filelist):
| if not isinstance(filelist, list):
| filelist = filelist,

Any what if 'filelist' is any iterable other than a string or list? Your
code is broken, and unnecessarily so. So I would call the parameter
'files' and test for isinstance(files, str) #or basestring. And wrap if it
is.

| for filename in filelist:
| f = open(filename,'r')
| #do interesting stuff with file, etc...
|
| ..and it's been working very well.
|
| Cheers,
| -Basilisk96
|
| --
| http://mail.python.org/mailman/listinfo/python-list
|

Jun 9 '07 #5
"Terry Reedy" <tjre...@udel.eduwrote:
Any what if 'filelist' is any iterable other than a string or list? Your
code is broken, and unnecessarily so. So I would call the parameter
'files' and test for isinstance(files, str) #or basestring. And wrap if it
is.
Can you give an example of such an iterable (other than a tuple)? I'd
certainly like to fix my 'fix' to work for a more general case.

Regards,
-Basilisk96

Jun 9 '07 #6
In <11**********************@q75g2000hsh.googlegroups .com>, Basilisk96
wrote:
"Terry Reedy" <tjre...@udel.eduwrote:
>Any what if 'filelist' is any iterable other than a string or list? Your
code is broken, and unnecessarily so. So I would call the parameter
'files' and test for isinstance(files, str) #or basestring. And wrap if it
is.

Can you give an example of such an iterable (other than a tuple)? I'd
certainly like to fix my 'fix' to work for a more general case.
def iter_filenames(filename):
lines = open(filename, 'r')
for line in lines:
yield line.rstrip()
lines.close()

filenames = iter_filenames('files.txt')

Now `filenames` is an iterable over strings representing file names but
it's not a `list`. And it's easy to come up with iterables over strings
that produce the data themselves, for example by attaching a counter to a
basename, or extracting the names from XML files, fetching them from a
database etc.

Ciao,
Marc 'BlackJack' Rintsch
Jun 9 '07 #7

"Basilisk96" <ba********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
| "Terry Reedy" <tjre...@udel.eduwrote:
| Any what if 'filelist' is any iterable other than a string or list?
Your
| code is broken, and unnecessarily so. So I would call the parameter
| 'files' and test for isinstance(files, str) #or basestring. And wrap
if it
| is.
|
| Can you give an example of such an iterable (other than a tuple)?

Tuple was the first thing I thought of, and one will break the list test.
The next would be an iterator that walks a file hierarchy spitting out the
names of non-directory files, or all files with a certain extension, or all
files with a certain owner, or timestamp characteristic.

| I'd certainly like to fix my 'fix' to work for a more general case.

As I said, I think it as simple as changing 'not list' to 'is string'.

tjr


Jun 9 '07 #8
Thank you both for clearing that up.
-Basilisk96

Jun 9 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by richbl | last post: by
14 posts views Thread by maverick | last post: by
10 posts views Thread by free2cric | last post: by
6 posts views Thread by Ole Nielsby | last post: by
4 posts views Thread by mab464 | last post: by
33 posts views Thread by Adam Chapman | last post: by
4 posts views Thread by arnuld | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.