-1

I need to generate the list of integers 1 to 100 (that's ok). But I need to then show a list of the first 10 numbers

print(" ")
for i in range(1, 11):
    print(list[i])
print(" ")

This is giving me list[1], then next line list[2] etc., instead of List [1,2,3,4,5,6,7,8,9,10]

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ado
  • 19
  • 3
    What did you _expect_ `list[i]` to do? – jonrsharpe Apr 15 '22 at 16:37
  • Where is `list` defined? Do you instead want to [`slice`](https://stackoverflow.com/questions/509211/understanding-slicing)? – C.Nivs Apr 15 '22 at 16:38
  • Does this answer your question? [Python 3 turn range to a list](https://stackoverflow.com/questions/11480042/python-3-turn-range-to-a-list) – Nin17 Apr 15 '22 at 16:56

3 Answers3

3

If you want only first ten numbers of list, do in this way:

list = [i for i in range(1,101)]
print(list[:10])

But if you want the list to be in multiples of 10 numbers using a loop, try in this way:

for i in range(10):
    print(list[10*i:10*(i+1)])
SAI SANTOSH CHIRAG
  • 2,046
  • 1
  • 10
  • 26
0

You're printing inside of a loop so it prints out something every time it runs. Don't know if it would work, just a thought, but maybe try appending to list in the loop then print the list outside, line4

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 15 '22 at 21:26
0

You can use this:

print(" ")
yourList = []
for i in range(1, 11):
    yourList.append(i)
print(" ")

print(yourList)