{"id":2532,"date":"2023-10-18T16:48:45","date_gmt":"2023-10-18T11:18:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=2532"},"modified":"2023-11-10T11:47:06","modified_gmt":"2023-11-10T06:17:06","slug":"how-to-create-and-initialize-a-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-create-and-initialize-a-list-of-lists-in-python\/","title":{"rendered":"How to Create and Initialize a List of Lists in Python?"},"content":{"rendered":"
Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.<\/p>\n
In Python, a list of lists is a list object with each list element being a separate list.<\/p>\n
Given the size,<\/p>\n
The task is to create and Initialize a list of lists\u00a0 of given size in Python using different methods.<\/p>\n
Examples:<\/strong><\/p>\n Input : <\/strong><\/p>\n Output :<\/strong><\/p>\n Below is the fastest way to build and initialize a list of lists with the same ID.<\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Here List of Lists of size 4 is created but we can see all are having same Id. This will result in the list containing the same list object repeated N times and cause referencing errors.<\/p>\n It is most preferable because all the lists will have different ID’s such that we can access and refer them separately.<\/p>\n There are several ways to create list of lists some of them are:<\/p>\n Using list comprehensions<\/strong> is the most Pythonic way to build an empty list of lists.<\/p>\n This can be accomplished in the following way:<\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Here we created list of lists of size 4, with different IDs for each sublist. \u00a0As a result, we can refer to them and initialise them separately.<\/p>\n We initialized second sublist with element 3.<\/p>\n Let’s say we want to make a list that includes 4 separate sub-lists.<\/p>\n To do so, we’ll first construct a new empty list, then use a for loop to iterate from 0 to 3 and add an empty list<\/p>\n to the new list for each iteration.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output :<\/strong><\/p>\n The repeat() function in the itertools module can be used to replace the range() function in the above list comprehension.<\/p>\n Below is the implementation<\/strong><\/p>\n Output:<\/strong><\/p>\n The empty() function in the Numpy module in Python creates an empty Numpy array of a given shape<\/p>\n It creates a new Numpy array of the specified shape. Below is the implementation:<\/strong><\/p>\n Note:<\/strong> This method performance is often slower than the list comprehension. Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. …<\/p>\nSize=4\r\n<\/pre>\n
[ [ ] , [ ] , [ ] , [ ] ]<\/pre>\n
Creating list of lists with the same ID(Not Preferable):<\/h2>\n
# Let us take size as 4\r\nlistoflists = [[]]*4 \r\n# print the listoflists \r\nprint(\"List of Lists : \", listoflists) \r\n# Print the ID's of all elements in this listoflists \r\nprint(\"ID's : \") \r\nfor element in listoflists: \r\n print(id(element))<\/pre>\n
List of Lists : [[], [], [], []]\r\nID's : \r\n140398466447368\r\n140398466447368\r\n140398466447368\r\n140398466447368<\/pre>\n
Creating list of lists with the different ID’s:<\/h3>\n
\n
Method #1: Using List Comprehension , range() and Append()<\/h3>\n
# Driver code\r\n# let us take size of list as 4\r\nsize = 4\r\n# Creating listoflists using list comprehension\r\nlistoflists = [[] for i in range(size)]\r\n# Printing empty listoflists\r\nprint(\"List of Lists :\", listoflists)\r\n\r\nprint(\"ID : \")\r\n\r\n# Printing the ID's of all sublists\r\nfor element in listoflists:\r\n print(id(element))\r\n\r\n# Initializing list of lists\r\n# let us initialize element in 2 nd sublist\r\nlistoflists[1].append(3)\r\n\r\n# printing list after initialization\r\nprint(\"List of Lists :\", listoflists)\r\n<\/pre>\n
List of Lists : [[], [], [], []]\r\nID : \r\n140259187390280\r\n140259187390024\r\n140259187390088\r\n140259187390152\r\nList of Lists : [[], [3], [], []]<\/pre>\n
Method #2 : \u00a0Using For loop and Append()<\/h3>\n
# Driver code\r\n# let us take size of list as 4\r\nsize = 4\r\n# Taking empty listoflists\r\nlistoflists = []\r\n# Iterate over a sequence of numbers from 0 to size\r\nfor i in range(size):\r\n listoflists.append([])\r\n\r\n# Printing empty listoflists\r\nprint(\"List of Lists :\", listoflists)\r\n\r\nprint(\"ID : \")\r\n\r\n# Printing the ID's of all sublists\r\nfor element in listoflists:\r\n print(id(element))\r\n\r\n# Initializing list of lists\r\n# let us initialize element in 2 nd sublist\r\nlistoflists[1].append(3)\r\n\r\n# printing list after initialization\r\nprint(\"List of Lists :\", listoflists)\r\n<\/pre>\n
List of Lists : [[], [], [], []]\r\nID : \r\n140629449111368\r\n140629449111240\r\n140629449111304\r\n140629449111176\r\nList of Lists : [[], [3], [], []]<\/pre>\n
Method #3: Using Itertools<\/h3>\n
# import repeat from itertools\r\nfrom itertools import repeat\r\n# let us take size of list as 4\r\nsize = 4\r\n# Creating listoflists using list comprehension\r\nlistoflists = [[] for i in repeat(None, size)]\r\n# Printing empty listoflists\r\nprint(\"List of Lists :\", listoflists)\r\n\r\nprint(\"ID : \")\r\n\r\n# Printing the ID's of all sublists\r\nfor element in listoflists:\r\n print(id(element))\r\n\r\n# Initializing list of lists\r\n# let us initialize element in 2 nd sublist\r\nlistoflists[1].append(3)\r\n\r\n# printing list after initialization\r\nprint(\"List of Lists :\", listoflists)\r\n<\/pre>\n
List of Lists : [[], [], [], []]\r\nID : \r\n140551299438408\r\n140551299438152\r\n140551299438216\r\n140551299438280\r\nList of Lists : [[], [3], [], []]<\/pre>\n
Method #4 : Using empty() in Numpy<\/h3>\n
numpy.empty(shape, dtype=float, order='C')<\/code><\/p>\n
\nNow, to generate a list of lists, we’ll use the empty() function to create a 2D Numpy array, which we’ll then transform to a list of lists using the numpy.tolist() function.<\/p>\n# importing numpy module\r\nimport numpy\r\n\r\n# let us take size of list as 4\r\nsize = 4\r\n\r\n# Create a 2D Numpy array of shape (4, 0) and convert it to list of lists\r\nlistoflists = numpy.empty((size, 0)).tolist()\r\n\r\n# Printing empty listoflists\r\nprint(\"List of Lists :\", listoflists)\r\n\r\nprint(\"ID : \")\r\n\r\n# Printing the ID's of all sublists\r\n\r\nfor element in listoflists:\r\n print(id(element))\r\n\r\n# Initializing list of lists\r\n\r\n# let us initialize element in 2 nd sublist\r\n\r\nlistoflists[1].append(3)\r\n\r\n# printing list after initialization\r\n\r\nprint(\"List of Lists :\", listoflists)\r\n<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n