[Tutor] append question

Luke Paireepinart rabidpoobear at gmail.com
Mon Jul 6 02:06:13 CEST 2009


Read your error message... It highlighted the first line of your for
loop ansd said ints aren't iterable. len(list) returns an integer. You
want a list of items... for i in range(len(list)):

On 7/5/09, Steven Buck <buckstec at gmail.com> wrote:
> Hi Python Tutors:
>
> I have a data structure that looks like:
>
>>>> test=[[1,2,3],[4,5,6],[7,8,9]]
>
> I want to define a new variable that captures the second element of each
> sublist from above:
>
>>>> testvar2 = []
>
> Next I try to capture the aforementioned elements:
>
>>>> for i in len(test):
>             testvar2.append(test[i][2])
>
> I want testvar2 = [2,5,8] but instead I get the following error message:
>
> Traceback (most recent call last):
>   File "<pyshell#34>", line 1, in <module>
>     for i in len(test):
> TypeError: 'int' object is not iterable
>
> Any insight would be appreciated.
> Thanks
> Steve
>
>
>
>
>
>
>
>
> --
> Steven Buck
> Ph.D. Student
> Department of Agricultural and Resource Economics
> University of California, Berkeley
>

-- 
Sent from my mobile device


More information about the Tutor mailing list