-2

How do I print every other item in a list without it giving me this error?

Im confused, I have tried this a million times in a million different ways, but same answer. My code right now:

item = 0
fruit = ['Strawberry','apple','pear','orange','banana','cucumber','tomato','Kiwi']
for count in fruit:
    print(fruit[item])
    item = item + 2

Any ideas?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Rhys Berry
  • 29
  • 1
  • 1
  • 3

4 Answers4

12

You may use slicing feature of list, i.e. you may slice your list content as:

your_list[start:end:step]

which in simple words will create a new list:

  • starting from index start
  • upto index end
  • with step of step

For more information, read: Python's slice notation

In your case, you can iterate over the sliced list with step of 2. You do not need index as a pointer to your list as your for will do it for you. Your code will look like this:

>>> fruit = ['Strawberry','apple','pear','orange','banana','cucumber','tomato','Kiwi']
>>>
>>> for f in fruit[::2]:
...     print f
...
Strawberry
pear
banana
tomato

Here fruit[::2] holds the value:

['Strawberry', 'pear', 'banana', 'tomato']

Edit: Reason for the failure of your code is, you are iterating over the entire list let's say of n length. But you are accessing the item at the twice the index of your current position. When you will be on n/2 th element, you will try to access item as fruit[n], which will raise IndexError as your list was holding item only upto n-1 th index.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
3

If you wish to iterate over fruit by skipping every 2 elements, you may proceed like below:

for item in fruit[::2]:
    print(item)
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
1

The reason your code doesn't work is because

for count in fruit:

iterates over each item in the fruit list, so once you're at the half-way point, item will point beyond the end of the list. Here's a modified version of your code to illustrate:

item = 0
fruit = ['Strawberry', 'apple', 'pear', 'orange', 'banana', 'cucumber', 'tomato', 'Kiwi']
print(len(fruit))
for v in fruit:
    print(v, item)
    item = item + 2

output

8
Strawberry 0
apple 2
pear 4
orange 6
banana 8
cucumber 10
tomato 12
Kiwi 14

So once we reach "banana", fruit[item] will try to access an item beyond the end of fruit, and that raises an IndexError exception.


As others have mentioned, the usual way to do this task in Python is to use extended slicing:

fruit = ['Strawberry', 'apple', 'pear', 'orange', 'banana', 'cucumber', 'tomato', 'Kiwi']

for v in fruit[::2]:
    print(v)

output

Strawberry
pear
banana
tomato

If you want the odd items, you can start the slice at 1:

for v in fruit[1::2]:
    print(v)

output

apple
orange
cucumber
Kiwi

Or you can just print a slice, as is, since it's just a list:

print(fruit[::2])

output

['Strawberry', 'pear', 'banana', 'tomato']
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

There was a slight mistake in your code in the print statment:

item = 0
fruit = ['Strawberry','apple','pear','orange','banana','cucumber','tomato','Kiwi']
for item in fruit[0::2]:
    print(item)

Output:

Strawberry
pear
banana
tomato
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44