Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
1

How to print every other item in a list

jn1003491
jn1003491

I have a list of names and I’m trying to get my program to print every other name on the list. I can’t figure out how to do this, so if anyone could help me it would be much appreciated.

2 years ago
You are viewing a single comment. View All
1
RYANTADIPARTHI
RYANTADIPARTHI

Solution

there is a very easy way to do this. Just use a slice method. And this works perfectly to print every other name. try this.

names = ["Peter", "Bruce", "Steve", "Tony", "Natasha", "Clint", "Wanda", "Hope", "Danny", "Carol"] print(names[::2])

bingo! That works. Like that.
That should work

2 years ago
1
jn1003491
jn1003491

Thanks again!

@RYANTADIPARTHI

2 years ago
1
jn1003491
jn1003491

What if I want to display only positive numbers?

@RYANTADIPARTHI

2 years ago
1
Coder39182
Coder39182

@jn1003491

for i in ARRAYNAME: if int(i) % 2 == 0: print(i)
2 years ago