# Converting List of String to Integer

Samir spython01 at gmail.com
Mon Jul 21 22:54:01 CEST 2008

```On Jul 21, 4:44 pm, Gary Herron <gher... at islandtraining.com> wrote:
> Samir wrote:
> > On Jul 21, 3:20 pm, Gary Herron <gher... at islandtraining.com> wrote:
>
> >> Samir wrote:
>
> >>> Hi Everyone,
>
> >>> I am relatively new to Python so please forgive me for what seems like
> >>> a basic question.
>
> >>> Assume that I have a list, a, composed of nested lists with string
> >>> representations of integers, such that
>
> >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>
> >>> I would like to convert this to a similar list, b, where the values
> >>> are represented by integers, such as
>
> >>> b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
>
> >>> I have unsuccessfully tried the following code:
>
> >>> n = []
> >>> for k in a:
> >>>     n.append([int(v) for v in k])
> >>> print n
>
> >>> Does anyone know what I am doing wrong?
>
>
> >>> Samir
> >>> --
> >>>http://mail.python.org/mailman/listinfo/python-list
>
> >> You didn't tell us how it failed for you, so I can't guess what's wrong.
>
> >> However, your code works for me:
>
> >>  >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
> >>  >>> n = []
> >>  >>> for k in a:
> >> ...    n.append([int(v) for v in k])
> >> ...
> >>  >>> print n
> >> [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
>
> >> (Although you seem to have confused variables b and n.)
>
> >> Gary Herron- Hide quoted text -
>
> >> - Show quoted text -
>
> > Hi Gary,
>
> > Thanks for your quick response (and sorry about mixing up b and n).
> > For some reason, the logic I posted seems to work ok while I'm using
> > the Python shell, but when used in my code, the program just hangs.
> > It never outputs the results.  Below is the code in its entirety.  Is
> > there a problem with my indendentation?
>
> Aha.  There's the problem, right there in the first line.
>
> > a = n = []
>
> This sets a and n to the *same* empty list.    This line creates one
> empty list and binds both n and a to that list.  Note carefully,  there
> is only one empty list here, but it can be accessed under two names
>
>
>   for k in a:
>
> runs through that list, and
>
>   n.append(...)
>
> append to the end of the same list.  Thus the loop never get to the end of the (continually growing) list.
>
> Solve it by creating two different empty lists:
>
>   a = []
>   n = []
>
> Gary Herron
>
>
>
> > t = """
> > 1 2
> > 3
> > 4 5 6
> > 7 8 9 0
> > """
>
> > d = t.split("\n")
>
> > for x in range(1,len(d)-1):
> >     a.append(d[x].split(" "))
> > print a
>
> > for k in a:
> >     n.append([int(v) for v in k])
>
> > print n
>
> > Thanks again.
>
> > Samir
> > --
> >http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Gary,

That did the trick!  I didn't realize that the way I initialized my
lists would lead to the behavior that I observed.  After doing
something similar to what John had suggested I did indeed discover
that I created an endless loop.  I'm glad I learned something today.