'int' object has no attribute 'getitem'

to_21=[i for i in range(1,22)]
odds=[to_21 for to_21 in range(1,22) if(to_21)%2!=0]
print odds
middle_third = to_21[8:15:1]
print middle_third

Traceback (most recent call last):
File “python”, line 4, in
TypeError: ‘int’ object has no attribute ‘getitem

take a look on this brother

Please post a link to the exercise. Thanks.

https://www.codecademy.com/courses/python-beginner-en-KAgt5/2/5?curriculum_id=4f89dab3d788890003000096#

Don’t need to use a loop. range returns an iterator.

to_21 = range(1, 22)

to_21 is an iterable (a list). This should be a simple variable…

odds = [x for x in to_21 if x % 2]

This exercise comes with an advanced lesson on slicing…

[ start : end : step ]

odds = [::2]

says start at beginning, end at end, step in two’s.

The last value is not needed…

middle_third = to_21[8:15]

tnx brother …it works…!!!:grinning:

1 Like

A post was split to a new topic: Remove_duplicates modifies the list that was passed to it