user2467266
user2467266

Reputation: 1

Looping a specific amount of times, each time making a new list

Basically what I want to do is have a program that makes a list based on user input, such as:

a=input
b=input
c=input

list1=[a,b,c]

then have it do it again (forming list2) and again and so on, until it reaches list37, where I want to make a list of lists, such as:

listMASTER=[list1,list2,list3...list36]

I don't want to write this:

a=input
b=input
c=input

listn=[a,b,c]

36 times, so I want it to loop over and over again with each time forming a new list.

Upvotes: 0

Views: 92

Answers (5)

jfs
jfs

Reputation: 414149

You could use nested loops:

list_of_lists = [[input() for _ in range(3)] for _ in range(36)]

Or more conveniently, also accept the input from a file e.g., using csv format:

a,b,c
d,f,g
...

Corresponding code:

import csv
import fileinput

list_of_lists = list(csv.reader(fileinput.input()))

Usage:

$ python make-list.py input.csv

Or

$ echo a,b,c | python make-list.py

Upvotes: 1

Rich Tier
Rich Tier

Reputation: 9441

master = [[input(),input(),input()] for i in xrange(37)]

Upvotes: 0

Mike Müller
Mike Müller

Reputation: 85442

A bit dense but still readable with a list comprehension

n = 36
prompt = 'Please enter for {}{}: '
all_inputs = [[input(prompt.format(char, num)) for char in 'abc'] 
               for num in range(n)]
print(all_inputs)

Gives you 36 x 3 input prompts:

Please enter for a0: 1
Please enter for b0: 2
Please enter for c0: 3
Please enter for a1: 4
Please enter for b1: 5
Please enter for c1: 6
...
[['1', '2', '3'], ['4', '5', '6'], ...]

Upvotes: 0

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

Use this way to do it easily:

olist=[]
for i in range(n): #n is the number of items you need the list (as in your case, 37)
    lis=[input(),input(),input()]
    olist.append(lis)

This will reduce the number of steps

Upvotes: 1

karthikr
karthikr

Reputation: 99620

Try something like this:

outer_listen = []
n = 36 #Or howmany ever times you want to loop
for i in range(n): #0 through 35
    a = input()
    b = input()
    c = input()
    lstn = [a, b, c]
    outer_listen.append(lstn)

Upvotes: 1

Related Questions