# Convert list to matrix in Python

Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data types. However, performing mathematical functions on lists is impossible due to which we need to convert the given lists to matrices or arrays in order to allow the successful performance of certain mathematical functions.

In this article, we will discuss how to convert list to matrix in Python.

Matrices are subclasses of arrays only and are therefore capable of inheriting all the methods and attributes of an array. There are multiple ways of converting a list to a matrix in Python, all of which have been described below in this article.

## Using the `while` loop to convert list to matrix in Python.

A matrix can also be described as a list of lists or nested lists.

The process of conversion of the given list to a list of lists can be successfully completed using a simple `while` loop, along with the use of list slicing and the `append()` method.

The following code uses the `while` loop to convert list to matrix in Python.

The above code provides the following output:

[[1, 3], [5, 7], [9, 11]]
##### Explanation:
• A list containing some elements is defined.
• The `while` loop is used on the list and helps in iterating it till the given list contains no elements.
• The list slicing process occurs (list slicing of 2 in this case)
• The append method is used to append the elements into a list of lists `m` defined in the code.

As you can see, a list can easily be converted into a matrix by changing it into a list of lists. However, a better and more reliable way to work on matrices in Python is using the `NumPy` library, which is described below.

## Use the `numpy.array()` method to convert list to matrix in Python.

`NumPy`, which is an abbreviation for Numerical Python, is a library that is mainly utilized to deal with matrices and arrays in Python.

The `numpy.array()` method is utilized in the creation and deletion of arrays in Python. It directly takes a list or a list of lists as an argument and returns a matrix.

To successfully implement this method, we will first need to import the `numpy` module to the Python code.

The following code uses the `numpy.array()` method to convert list to matrix in Python.

The above code provides the following output:

[ 1 3 5 7 9 11]

## Using the `numpy.asarray()` method to convert list to matrix in Python.

Similar to the `numpy.array()` method mentioned above, the `numpy.asarray()` method is utilized to convert different data type objects like lists, dictionaries, and more into `NumPy` matrices. The `numpy.asarray()` basically calls the `numpy.array()` method within itself, which makes the syntax of the former function just a simple extension of the latter.

The only difference between the two functions provided by the `NumPy` module is that in the `numpy.asarray()` method, the `copy` flag is set to `false` by default, while it is `true` in the case of `numpy.array()`.

The following code uses the `numpy.asarray()` method to convert list to matrix in Python.

The above code provides the following output:

[ 1 3 5 7 9 11]

Here, the `numpy.asarray()` does not make a copy of the object and converts the given list straight into a `NumPy` matrix.

That’s all about how to convert List to matrix in Python.

import_contacts

## Related Posts

• 27 December

### Create Array of All NaN Values in Python

Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63da610449058903453328/] [crayon-63da61044905f012209718/] We used numpy.empty() […]

• 27 December

### Call Function from Another Function in Python

Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63da6104492a7153004417/] [crayon-63da6104492ac723116039/] To call a function, […]

• 27 December

### [Fixed] NameError Name ‘unicode’ is Not Defined in Python

Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63da610449405151303957/] [crayon-63da610449409759872926/] Unicode is a computing industry standard that ensures that text from most of […]

• 26 December

### Repeat List N Times in Python

Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

• 26 December

### Get First Day of Next Month in Python

Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

• 25 December

### Convert String Array to Int Array in Python

Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.