In [1]: test=[[1,2,3],[4,5,6],[7,8,9]] In [3]: testvar2 = [] In [16]: for i in range(len(test)): ....: testvar2.append(test[i][1]) ....: ....:
In [17]: testvar2 Out[17]: [2, 5, 8] Robert On Sun, 2009-07-05 at 15:57 -0700, Steven Buck 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 > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor