-5

New to Python and, programming as a whole, and not completely sure if this is possible but is there a way to create a "number" of lists based on an inputted "number"?

Code should function as follows:

  1. Ask user how many lists they want to create (Ex. user types 3)
  2. Code will create three separate lists (Ex. list1 = [], list2 = [], list3 = [])

Note: I need lists specifically. No dictionaries, global, etc.

curiousgeorge
  • 111
  • 1
  • 12

3 Answers3

1

One simple way to accomplish this is to simply store lists in a list of lists as such

num_lists = int(input('How many lists?'))
lists = [[] for i in range(num_lists)]

Then each list can be accessed by an index (for example list1 = lists[0]).

nico
  • 2,000
  • 4
  • 22
  • 37
1

here is a solution

num = int(input('How Many?: '))
all = {'list'+str(i+1):[] for i in range(num)}
print(all)
print(all['list1'])

sample output

How Many?: 3
{'list2': [], 'list3': [], 'list1': []}
[]
bvmcode
  • 2,182
  • 18
  • 32
  • I guess I should've initially specified I didn't want to use dictionaries. Nonetheless, I see the code is essentially the same. Thanks for the future reference!!! – curiousgeorge Apr 11 '18 at 20:28
0
lists=[[] for i in range(int(input('enter the number of list : ')))]

then you can access the list as index

first list

list1 =lists[0]
Roushan
  • 3,558
  • 3
  • 20
  • 37