Ask coding questions

← Back to all posts
don't know on python

Ask the user for 5 integers and store them in a list.
Find and output all its elements with even indices (i.e. A[0], A[2], A[4], ...) on the same line with a space between them.


Looks like a homework question, which means getting a 'solution' from someone here won't fix your actual problem (not feeling confident in using lists to store multiple values).

It will in fact do the opposite, because submitting a solution you didn't write and don't understand makes your teacher think you HAVE understood, so they won't (don't need to) help you further with this aspect of Python. They will instead give you harder problems to solve, built on the learning you (apparently) gained on this one.

Teachers are (or should be) like doctors - they 'perform tests' to 'diagnose' what's 'wrong' then 'fix' you (with nudges that help you move closer to solving the thing and learning that aspect). If you get code from here, you're effectively telling the docter you're fine (when you're not), and need no help (when you do).

The correct answer to your post would be to watch videos or read a walkthroughs on using lists in Python.

BTW I'm a hrgh school CS teacher, so I see kids missing this crucial point every day.

It's like getting your friend to beat some level on a game, but that level is ACTUALLY teaching you how to use weapon X, or special move Y, and if your friend does it for you, you'll find the following levels so much harder, cos the developer (who is 'teaching' you the game!) wouldn't have let you get to the boss battle til you proved you'd mastered the 'beatdown' move or the 'slow down time so you can tag 5 enemies in one second' effect.

From a teaching perspective, the solutions you've been offered are WAY too complex for you to understand since you've already told us you don't understand lists.

Break down the problem until you can do each part , then reassemble them into a whole:

Ask the user for 5 integers and store them in a list.

I assume you can do this with a looped nums.append( input() )

Find and output all its elements with even indices (i.e. A[0], A[2], A[4], ...)

A second loop through the list items, this time using the third parameter of 'range' to skip/jump in twos...

on the same line with a space between them.

This can either be done by supressing print's auto-linebreak with the optional END
parameter (look it up) or by gathering all the values into a new text string then printing that.

NB Before anyone objects to looping twice through the data, this is a LEARNING exercise, where brain space is at a premium. We're NOT writing production code, just trying to help a noob wrap their head around a 3-step problem.


@JohnKershaw so ur saying I'm not smart how dare you say that

nums = []
for a in range(5):
  nums.append(int(input('Enter an Integer: ')))
for b in range(len(nums)):
  if b % 2 == 0:
    print(nums[b], end=' ')

@Geocube101 Excellent code, clearly expressed. However the original question is looking for even indices, not even values, so

for b in range(0, 5, 2):

Reason being it's unlikely the original poster has met the % operator yet, as it's a tricky thing for the less mathematically minded and tends to get in the way of the teaching point (though, obviously, super important for lots of things).

Also, I'd insert blank lines before the 'for' blocks, for clarity of understanding.


@JohnKershaw The second for loop iterates over the length of the list. Because of this, the variable b represents the index value for an element in the array. If the index is even, the corresponding value is printed.


@Geocube101 So it does, and your code is technically perfect.

I would still avoid the % operator - and range(len(nums)), as nested functions add mental overhead the OP likely cannot spare.


@Geocube101 i get what you are doing but how i would do it for even numbers


@SajidVijayan3 The modulus (%) operator returns the remainder of two numbers
Because any even number divided by 2 has a remainder of 0, the seconds for loop and if statement only accepts even numbers


The division operator / returns the quotient of the left divided by the right
The Modulus operator '%' returns the remainder of the left divided by the right

Example: Take 50 % 2. Using Modulus, python divides 50 by 2 and returns the amount left. Because any even number divided by 2 has no numbers remaining, this returns 0. On the other hand, 17 is odd. Because dividing any odd number by 2 leaves 1 remaining, the expression 17 % 2 returns 1.

Remainders work by seeing how many times a number can divide into another number perfectly (with no decimals). The remainder is the amount left when a number reaches the maximum amount of times it is capable of dividing into another number.
17 % 2; 2 goes into 17 eight times; 2 times 8 is 16; 17-16 is 1 so remainder is 1


Using comprehension lists

int_list = []
[int_list.append(input("Give me a number: ")) for x in range(5)]
list_even = [x for x in int_list if int_list.index(x) % 2 == 0]
print(*list_even, sep=" ")


Give me a number: 1
Give me a number: 2
Give me a number: 3
Give me a number: 4
Give me a number: 5
[None, None, None, None, None]
['1', '3', '5']
1 3 5

@EducationalChan Hi !
Your solution do not work with the following input : 1 2 2 4 5

Why not

int_list = [input("Give me a number: ") for x in range(5)]
list_even = [int_list[x] for x in range(0,5,2)]
print(*list_even, sep=" ")



It would be better if you give an example or explain it further
Would this work: Repl

import sys

A = []

for i in range(1,6):
  a = input("Integer " + str(i) + ': ')
c = 0
for e in A:
  sys.stdout.write(e+"["+str(c)+"]"+" ")
  c += 1

If inputed: 1,2,3,4,5 the output would be:

1[0] 2[1] 3[2] 4[3] 5[4]

sys.stdout.write() allows you to print things on the same line right after each other. You have to add the " " (extra space) because they will print right after each other. If you want them touching then remove the space. The print() function prints a new line each time.

If this is not the answer you want then please give examples of what you want so that I can help.

If my reply helped you please mark this as the answer. Thank you


@Pythonier This is very nice code, but pulling in the sys module is much 'further on' for someone who can't yet loop through a list in jumps of two. The 'print' function's end=' ' parameter is the way to go here.


2 ways you could do that:
1) list.append(int(input()) 5x
2) list = input().split(<your divider, eg:,)


@AllAwesome497 These are both excellent solutions, but not for this question, since their 'cleverness' rating is WAAY higher than the original poster's level of understanding - if they could understand your code, they wouldn't be asking how to loop through a list's even indices. The OP includes the person posting, not just the problem/solution itself.