How do you create a large list in python?

This article describes how to initialize a list with any size [number of elements] and values in Python.

  • Create an empty list
  • Initialize a list with any size and values
  • Notes on initializing a 2D list [list of lists]
  • For tuples and arrays

See the following article about the initialization of NumPy array ndarray.

  • NumPy: Create an ndarray with all elements initialized with the same value

Create an empty list

An empty list is created as follows. You can get the number of elements of a list with the built-in function len[].

l_empty = []
print[l_empty]
# []

print[len[l_empty]]
# 0

You can add an element by append[] or remove it by remove[].

l_empty.append[100]
l_empty.append[200]
print[l_empty]
# [100, 200]

l_empty.remove[100]
print[l_empty]
# [200]

See the following articles for details on adding and removing elements from lists,

  • Add an item to a list in Python [append, extend, insert]
  • Remove an item from a list in Python [clear, pop, remove, del]

Initialize a list with any size and values

As mentioned above, in Python, you can easily add and remove elements from a list, so in most cases, it is not necessary to initialize the list in advance.

If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the * operator as follows.

l = [0] * 10
print[l]
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

print[len[l]]
# 10

A list is generated that repeats the elements of the original list.

print[[0, 1, 2] * 3]
# [0, 1, 2, 0, 1, 2, 0, 1, 2]

You can generate a list of sequential numbers with range[].

  • How to use range[] in Python

Notes on initializing a 2D list [list of lists]

Be careful when initializing a list of lists.

The following code is no good.

l_2d_ng = [[0] * 4] * 3
print[l_2d_ng]
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

If you update one list, all the lists will be changed.

l_2d_ng[0][0] = 5
print[l_2d_ng]
# [[5, 0, 0, 0], [5, 0, 0, 0], [5, 0, 0, 0]]

l_2d_ng[0].append[100]
print[l_2d_ng]
# [[5, 0, 0, 0, 100], [5, 0, 0, 0, 100], [5, 0, 0, 0, 100]]

This is because the inner lists are all the same object.

print[id[l_2d_ng[0]] == id[l_2d_ng[1]] == id[l_2d_ng[2]]]
# True

You can write as follows using list comprehensions.

  • List comprehensions in Python

l_2d_ok = [[0] * 4 for i in range[3]]
print[l_2d_ok]
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Each inner list is treated as a different object.

l_2d_ok[0][0] = 100
print[l_2d_ok]
# [[100, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

print[id[l_2d_ok[0]] == id[l_2d_ok[1]] == id[l_2d_ok[2]]]
# False

Although range[] is used in the above example, any iterable of the desired size is acceptable.

l_2d_ok_2 = [[0] * 4 for i in [1] * 3]
print[l_2d_ok_2]
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

l_2d_ok_2[0][0] = 100
print[l_2d_ok_2]
# [[100, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

print[id[l_2d_ok_2[0]] == id[l_2d_ok_2[1]] == id[l_2d_ok_2[2]]]
# False

If you want to generate a multidimensional list, you can nest list comprehensions.

l_3d = [[[0] * 2 for i in range[3]] for j in range[4]]
print[l_3d]
# [[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]

l_3d[0][0][0] = 100
print[l_3d]
# [[[100, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]

For tuples and arrays

You can initialize tuples as well as lists.

Note that a tuple with one element requires ,.

  • A tuple with one element requires a comma in Python

t = [0,] * 5
print[t]
# [0, 0, 0, 0, 0]

For array type, you can pass the initialized list to the constructor.

  • array — Efficient arrays of numeric values — Python 3.9.0 documentation

import array

a = array.array['i', [0] * 5]
print[a]
# array['i', [0, 0, 0, 0, 0]]

How do I make a list of 100 items in Python?

Python Create List from 0 to 100. A special situation arises if you want to create a list from 0 to 100 [included]. In this case, you simply use the list[range[0, 101]] function call.

Is there a Max list size in Python?

According to the source code, the maximum size of a list is PY_SSIZE_T_MAX/sizeof[PyObject*] . On a regular 32bit system, this is [4294967295 / 2] / 4 or 536870912. Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.

How do I make an n list size in Python?

To create a list of n placeholder elements, multiply the list of a single placeholder element with n . For example, use [None] * 5 to create a list [None, None, None, None, None] with five elements None . You can then overwrite some elements with index assignments.

How do I make a list of 10 elements in Python?

A list is generated in Python programming by putting all of the items [elements] inside square brackets [], separated by commas. It can include an unlimited number of elements of various data types [integer, float, string, etc.]. Python Lists can also be created using the built-in list[] method.

How do you create a list in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types [integer, float, string, etc.]. A list can also have another list as an item.

How do you size a list in Python?

Python has got in-built method – len[] to find the size of the list i.e. the length of the list. The len[] method accepts an iterable as an argument and it counts and returns the number of elements present in the list.

Chủ Đề