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
JBloves27
JBloves27

Hello,
To print all you just do the following:

print(names) print(numbers)

However, to do certain ones, like you said every other one, you do the following:

rand_num=0 for i in names: print(names[rand_num]) rand_num+=2

I hope this helps!

2 years ago
1
Coder39182
Coder39182

@JBYT27
the second example would break since you would add 2 to rand_num 9 times since you're iterating through the for loop for every item in names (there are nine items), meaning it would attempt to print names[18] which does not exist.

2 years ago
1
JBloves27
JBloves27

oh. then your way prob works XD

@JoeyRueff

2 years ago