Python | Initializing multiple lists

Python Methods and Functions

Method # 1: Using the * operator
We can list all the required commas in the list separated by commas, and then initialize them with an empty list and multiply this empty list using the * operator by the number of specified lists .

 ` # Python3 demo code ` ` # to initialize multiple lists ` ` # using the * operator `   ` # using * operator ` ` # to initialize multiple lists ` ` list1, list2, list3 , list4 ` ` = ` ` ([],) ` ` * ` ` 4 `   ` # printing lists ` ` print ` ` (` ` "The initialized lists are:" ` `) ` ` print ` ` (` `" List 1: "` ` + ` ` str ` ` (list1)) ` ` print ` ` (` ` "List 2:" ` ` + ` ` str ` ` (list2)) ` ` print ` ` (` ` "List 3:" ` ` + ` ` str ` ` (list3)) ` ` print ` ` (` ` "List 4:" ` ` + ` ` str ` ` (list4)) `

Output:

` The initialized lists are: List 1: [] List 2: [] List 3: [] List 4: [] `

Method # 2: Using a loop
This method is similar to the method described above, but the only difference is that we use a loop instead of the * operator to perform a multi-assignment task.

 ` # Python3 demo code ` ` # to initialize multiple lists ` ` # using a loop `   ` # using a loop ` ` # to initialize multiple lists ` ` list1, list2, list3, list4 ` ` = ` ` ([ ] ` ` for ` ` i ` ` in ` ` range ` ` (` ` 4 ` `)) `   ` # print lists ` ` print ` ` (` ` "The initialized lists are:" ` `) ` ` print ` ` (` ` "List 1:" ` ` + ` ` str ` ` (list1)) ` ` print ` ` (` ` "List 2:" ` ` + ` ` str ` ` (list2)) ` ` print ` ` (` ` " List 3: "` ` + ` ` str ` ` (list3)) ` ` print ` ` (` ` "List 4:" ` ` + ` ` str ` ` (list4)) `

Exit:

` The initialized lists are: List 1: [] List 2: [] List 3: [] List 4: [] `

Method # 3: Using `defaultdict()`
This method is different and also has several other usefulness than the two methods discussed above. This creates a dictionary with a specific name, and we have the ability to create any number of keys and perform add operations right away, since they are initialized by a list.

` `

 ` # Python3 demo code ` ` # to initialize multiple lists ` ` # using defaultdict () ` ` import ` ` collections `   ` # using defaultdict () ` ` # to initialize multiple lists ` ` # no need to initialize with empty lists ` ` mul_list_dict ` ` = ` ` collections.defaultdict (` ` li st ` `) ` ` mul_list_dict [` ` 'list1' ` `]. append (` ` 1 ` `) ` ` mul_list_dict [` ` 'list2' ` `]. append (` ` 2 ` `) ` ` mul_list_dict [` `' list3' ` `]. append (` ` 3 ` `) ` ` mul_list_dict [` ` 'list4' ` `]. append ( ` ` 4 ` `) `   ` # printing lists ` ` print ` ` (` ` "The initialized lists are:" ` `) ` < / p> ` print ` ` (` ` "List 1:" ` ` + ` ` str ` ` (mul_list_dict [` ` 'list1 '` `])) ` ` print ` ` (` ` "List 2:" ` ` + ` ` str ` ` (mul_list_dict [` ` 'list2' ` `])) ` ` print ` ` (` ` "List 3:" ` ` + ` ` str ` ` (mul_list_dict [` ` 'list3' ` `])) ` ` print ` ` (` `" List 4: "` ` + ` ` str ` ` (mul_list_dict [` ` 'list4' ` `] )) `

` ` Exit:

` The initialized lists are: List 1: [1] List 2: [2] List 3: [3] List 4: [4] `