Creating a list of lists in python is a little tricky. In this article, we will discuss 4 different ways to create and initialize list of lists.
Make A List C++
Wrong way to create & initialize a list of lists in python
Create a Unique List in Excel based on Criteria. February 23, 2017. Nathan is working on a spreadsheet that contains a list of car models and owners. He needs to create a unique list of owners per car. “Maybe I can use an IF formula,” he thinks to himself and decides to give it a go. However, after several failed attempts he decides to. Quickly and easily make a list. Drag your list items around or throw them in the trash if you don't like them. You can then email your finished list or print it on paper.
Let’s start from basic, quickest way to create and initialize a normal list with same values in python is,
Output:
It created a list of size 10 with the same element i.e. 5. Basically it copied the object inside [], 10 times. Let’s use same logic to create and initialize a list of lists,
Output:
Here we provided an empty list [] inside the [] and multiplied it by 4. It created 4 different references of [] and inserted them into a new list. Yes, these are not 4 empty lists inside the main list, but these are not different list objects instead these are mere references of first list. Let’s confirm this by iterating over the list and printing id of each sub list object,
Output:
All entries in the list have the same ID i.e. they point to the same list object.
Why does it matter ?
Let’s inserting an element in the 3rd sub list of main list i.e.
Now check the contents of the main list,
Output:
Element was inserted in all the sub lists, because these are not different lists. We didn’t expect this at first place, we just wanted to insert the element in the 3rd sub-list. So, it proves that this is a wrong way to create and initialize a list of list. Let’s look at the correct way,
- So you make a variable, name it what you'd name a list, in this case, 'list.' Set it to a list, I've found out that the name really doesn't matter that much. To get the equivelant of 'add (thing) to list v' on scratch: instead of using you'd use the only time you'd use is if you have more than 1 list you'd want to set list to.
- Create a print only list Go to Home and select the down arrow next to the Bullets button. In the drop down menu, select Define New Bullet. Select Symbol and find a box character.
Correct way to Create & Initialize a list of lists in python
Let’s see 4 different but correct ways to create and initialize a list of lists
Use for loop to Create & Initialize list of lists
Suppose we want to create a list, which internally contains 5 different sub lists. To do that, first we will create an new empty list,then we will iterate from numbers 0 to 4 using a for loop and in each iteration, we will append an empty list to the new list i.e.
Output:
Now let’s confirm that if all the sub lists in main list are actually different lists,
Output:
All the sub lists have different IDs, which confirms that these are different objects.
Let’s insert the element in the 3rd sub list,
Output:
Element is added only in the 3rd sub list, all other sub lists are not affected. So, it also confirms that all the sub lists are not the references of the same list, instead they are completely different objects.
Use List Comprehension & range() to create a list of lists
Using Python’s range() function, we can generate a sequence of numbers from 0 to n-1 and for each element in the sequence create & append a sub-list to the main list using List Comprehension i.e.
Output:
It created a list with 5 sub lists. Let’s confirm if each sub-list is a different object or not,
Output:
It proves that all sub lists have different Identities.
Use List Comprehension & repeat() to create a list of lists
In python itertools module provide a function repeat(),
It returns an Iterator which in turns returns the given object N times.
We can use it for basic iteration from 0 to 4 and in each iteration we will append a sublist to main list i.e.
Output:
It returned a list with 5 sub-lists. We can also confirm that all the sub lists are different object,
Output:
It proves that all sub lists have different Identities.Although, this is one of the least used method to create a list of lists, but still it is good to know different things because once you know the APIs and their use cases, then you can apply it to some other scenarios.
Use Numpy to create a list of lists
Numpy module in python provides a function empty() to create a empty Numpy array of given shape i.e.
It returns a new Numpy array of given shape.
So, now to create a list of lists, we will create a 2D Numpy array using empty() function and will then convert it into a list of lists using numpy.tolist() function. For example,
Output:
So, we create a list that has 5 sublists. Now let’s confirm that all the sub lists are different object,
Output:
It proves that all sub lists are different objects.
The complete example is as follows,
Output:
Related Posts:
Returns a dynamic
(JSON) array of all the values of Expr in the group.
- Can be used only in context of aggregation inside summarize
Syntax
summarize
make_list(
Expr [,
MaxSize])
Arguments
- Expr: Expression that will be used for aggregation calculation.
- MaxSize is an optional integer limit on the maximum number of elements returned (default is 1048576). MaxSize value cannot exceed 1048576.
Note
makelist()
is a legacy and obsolete version of the make_list
function. The legacy version has a default limit of MaxSize = 128.
Returns
Returns a dynamic
(JSON) array of all the values of Expr in the group.If the input to the summarize
operator is not sorted, the order of elements in the resulting array is undefined.If the input to the summarize
operator is sorted, the order of elements in the resulting array tracks that of the input.
Tip
Use the array_sort_asc()
or array_sort_desc()
function to create an ordered list by some key.
Examples
One column
The simplest example is to make a list out of a single column:
mylist |
---|
['triangle','square','rectangle','pentagon','hexagon','heptagon','octogon','nonagon','decagon'] |
Using the 'by' clause
In the following query, you group using the by
clause:
mylist | isEvenSideCount |
---|---|
false | ['triangle','pentagon','heptagon','nonagon'] |
true | ['square','rectangle','hexagon','octogon','decagon'] |
Packing a dynamic object
You can pack a dynamic object in a column before making a list out of it, as seen in the following query:
List Creator
mylist | isEvenSideCount |
---|---|
false | [{'name':'triangle','sideCount':3},{'name':'pentagon','sideCount':5},{'name':'heptagon','sideCount':7},{'name':'nonagon','sideCount':9}] |
true | [{'name':'square','sideCount':4},{'name':'rectangle','sideCount':4},{'name':'hexagon','sideCount':6},{'name':'octogon','sideCount':8},{'name':'decagon','sideCount':10}] |
See also
Make A List Chart
make_list_if
operator is similar to make_list
, except it also accepts a predicate.