Matrices are not represented by any built-in data type in Python, but may be represented as a nested list, or a list of lists. Create a list and use a “while” loop, the “append” method and list slicing to convert the list to a matrix. This is a good programming exercise, if not a mathematically meaningful one. list = [1,2,3,4,5,6,7,8,9] matrix = [] while list != []: The loop does not execute immediately, and any following commands preceded by a tab character become part of the loop. matrix.append(list[:3]) This command slices the first three items from the list and adds them to the matrix. list = list[3:] This command removes the first three items from the list, so that on the next iteration of the loop, the fourth, fifth and sixth items are sliced and added to the matrix, and so on.

How to Convert a List to a Matrix in Python - 42