-1

I have been working for a small program that in the end creates a list with 100 randomized numbers in it. I am supposed to print the list in a 10x10 space, and, since I'm a newbie in code I can't really wrap my head around how to do it. Any suggestions?

could look like this:

list1 = [0]*100

and end result should look like this:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Thanks in advance!

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
  • For-loops should help here. What have you tried so far? – mrEvgenX Jan 22 '20 at 07:18
  • What's supposed to happen if you have all zeroes and one item is a 10? Are all entries supposed to be aligned, or do you just want to print one 10-items sublist per line? – Masklinn Jan 22 '20 at 07:19
  • Take a look at the [random](https://docs.python.org/3/library/random.html) module – Devesh Kumar Singh Jan 22 '20 at 07:19
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] of your own attempt and show it to us in the question itself, together with a description of the problems you have with it. – Some programmer dude Jan 22 '20 at 07:19
  • 1
    Please show us what you have tried so far. – MrLeeh Jan 22 '20 at 07:24
  • Have you considered structuring your list as a 10x10 list of lists instead of one 100 items list? This will make the printing super easy – Tomerikoo Jan 22 '20 at 07:33

3 Answers3

0

you can achieve this with two for loops. something like this:

import random
final_list =[]
for i in range(0, 10):
    row_list = []
    for j in range(0, 10):
        row_list.append(random.randint(1, 10000))
    final_list.append(row_list)

break down:

  1. you could use a library called random for creating a random number. you can look at its document here.
  2. in random.randint(1, 10000) I gave a range which is the range of random numbers. you could change it to suit your requirements.
  3. there might be some better way to do this but it's more understandable :)
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
0

what you need is just the built-in function print:

print(*(list1[i: i + 10] for i in range(0, len(list1), 10)), sep='\n')

output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

maybe the code is a bit complicated since you are trying to learn but I will try to explain:

  • there is a generator that splits your initial list into smaller lists of 10 elements:

    (list1[i: i + 10] for i in range(0, len(list1), 10))

  • then I'm using unpack operator * to unpack the generator into print arguments
  • I'm separating each list by a new line using sep='\n'

a more simple version to achieve your end result is to use a single for loop:

for index in range(0, len(list1), 10):
    print(list1[i: i + 10])

ouptu:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • I sadly dont understand the code so i will not be using this, but still, thanks you for the suggestion and for trying to explain it! – flexan_kodar Jan 22 '20 at 08:37
  • @flexan_kodar what about now? I added a more simple solution – kederrac Jan 22 '20 at 08:54
  • Thank you, now it's easier for me to get behind! Im still a lttle confused about what list1[i: i + 10] does but i think i have some general understanding. – flexan_kodar Jan 22 '20 at 09:07
  • list1[i: i + 10] is using slicing, "cutting" the list at a given start-end position in list, you can have a look here : https://stackoverflow.com/questions/509211/understanding-slice-notation – kederrac Jan 22 '20 at 09:10
  • I will have a look at that, thank you again for the help! – flexan_kodar Jan 22 '20 at 10:09
-1

I have pretty much solved the problem, at least to my satisfacion, it looks like this:

example_list = [0]*100

index_print = 0

for x in range(10):
    for y in range(10):
        print(example_list[index_print], end=' ')
        index_print += 1
    print('\n')

Could be inporved, and i will probably do that too, but right now im satisfied. Sadly i have discovered some problems with my code that i have not solved yet, but thats for anoher question. Thanks for the many answers, would not have expected to get so many on my first post!