You can construct a list in a certain number of ways as detailed in the
Python documentation
. The most popular I use the most is by defining my list variables using the
[]
list square bracket notation, for example as
my_list = []
.
This works well when you want to start a list empty, but what if you want to be able to start with a list so that it is prepopulated with values?
The easiest way to create a list prepopulated with values is by defining the list with the values and applying the multiplication operator for the number of elements you want your lists to start with. For example, to have a list of ten items with each value in the list being zero, do the following:
my_list = [0] * 10
Here’s how this looks:
> my_list = [0] * 10
> print(my_list)
> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> my_list = [""] * 5
> print(my_list)
> ['', '', '', '', '']
> my_list = [{}] * 3
> print(my_list)
> [{}, {}, {}]
As you can see from the above example, I now have a list with each element containing zero values, or empty strings, or with some other value such as a dictionary.
How About Different First Value & Then Same Values Everything Else?
But what if the first value in the initial list needs to be different, but all other values in the list are the same?
We can combine two lists using the list concatenation technique (using the
+
sign between two lists) and have the second list represent the prepopulated lists explored above.
Here are some examples:
> my_list = ["Employee"] + [0] * 12
> print(my_list)
> ['Employee', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> my_list = ["ID"] + ["Employee"] + [0] * 3
> print(my_list)
> ['ID', 'Employee', 0, 0, 0]
> my_list = ["ID", "Employee"] + [0] * 3
> print(my_list)
> ['ID', 'Employee', 0, 0, 0]
> my_list = ["Name"] * 2 + ["Address"] * 3
> print(my_list)
> ['Name', 'Name', 'Address', 'Address', 'Address']
In the examples listed above, you can see the simple concatenation of a list that contains one element with a list containing 12 elements prepopulated with zeroes. The next two examples are equivalent – you can either concatenate lists together or have a prepopulated list with the exact values you need, before combining it with the prepopulated list multiplier.
The last example above combines two multiplied lists together to form a multi-element prepopulated list.
So as you can see there are no limits to prepopulating lists in Python and you can use a plethora of combinations when combining the multiplier and the concatenation operators to lists.
Creating List With Different Values For Each Element
Is there a way to prepopulate a list in Python where each value is based on some mathematical relationship, for example, each value increments by 1?
You can use the above techniques with list comprehensions to create a list of values where the value of the prepopulated list increments (or decrements) according to however you wish.
Here are some ideas to get you started:
> my_list = [idx for idx, x in enumerate([None] * 3)]
> print(my_list)
> [0, 1, 2]
This technique is important in several elements, however, if the list in the
enumerate
function is not populated with
some
value the technique will
NOT
work at all, as evidenced by the following example:
> my_list = [idx for idx, x in enumerate([] * 3)]
> print(my_list)
> []
Knowing that you can use an increment (or decrement) on the values with this technique allows for many means to get a list prepopulated.
Here are some more examples:
> my_list = [x ** idx for idx, x in enumerate([2] * 3)]
> print(my_list)
> [1, 2, 4]
> my_list = [(idx, x) for idx, x in enumerate([0] * 3)]
> print(my_list)
> [(0, 0), (1, 0), (2, 0)]
> my_list = [{idx: x} for idx, x in enumerate([""] * 3)]
> print(my_list)
> [{0: ""}, {1: ""}, {2: ""}]
As you can see from the fairly simple examples above there is a lot of scope with being able to prepopulate a list with different types of values and data types, and for certain needed values increasing (or decreasing) within the list – and all from one line of code!
Summary
There are several ways to create a list in Python with a set of values already prepopulated. The most popular technique is to use the multiplication operator with the list square bracket notation provided the list defined contains some value (i.e. cannot be empty).
When prepopulating lists you can continue being creative by using list concatenation, and move even further when using list comprehensions.