How To Define Array In Python?

First of all, Python does not support the array type. Actually, Python provides a very similar type name list which is the same as an array. But the type array is not supported exactly. As the list type is very similar to the array type in this tutorial we examine the list type and how to use it like an array.

Define Array

The syntax of the list type is like below where the items are put inside the square brackets by separating them with commas. In the following example, we define an array named names and add some items to it.

names = ["ismail","ahmet","ali"]

Define Empty Array

We can define an empty array using the list type. We do not add any item into the square brackets.

names = []

Define Nested Array

Sometimes we may need to store complex data in a single array where there will be some sub-arrays or child arrays. This structure is called a nested array where an array item is another array. We can define a nested array in Python like below. In the following example, we define a nested array named names which contain names and surnames.

names=[ ["ismail","baydan"] , ["ahmet","ali","baydan"] ]

Leave a Comment