If you fix the lenght of the array, you can just create a list with a none value
keyword = [''] * 100 # list of 100 element
keyword[0] = 'Blue Light Glasses'
keyword[1] = "Tech Fleece Joggers"
You can also create a empty list and add element in this list
keyword = list()
keyword.append('Blue Light Glasses')
keyword.append('Tech Fleece Joggers')
print(keyword)
Output:
['Blue Light Glasses', 'Tech Fleece Joggers']
Answer from Shrmn on Stack OverflowIf you fix the lenght of the array, you can just create a list with a none value
keyword = [''] * 100 # list of 100 element
keyword[0] = 'Blue Light Glasses'
keyword[1] = "Tech Fleece Joggers"
You can also create a empty list and add element in this list
keyword = list()
keyword.append('Blue Light Glasses')
keyword.append('Tech Fleece Joggers')
print(keyword)
Output:
['Blue Light Glasses', 'Tech Fleece Joggers']
there isn't a way in python to declare a array of a fixed lenght but there are some "tricks" to do this as:
lenght = 100
# You are creating an array of 100 None items
listOfNone = [None] * lenght
Another thing you can't do is declare an array of that fixed value type (like string). You can create an array only made of string but at any time you could replace a string element of the array with an int for example. Here is an example:
lenght = 100
# Declaring an array made of 100 "a beautiful string" repeating
listOfStrings = ["a beautiful string"] * lenght
# Replacing the tenth element with a 12 (type int)
listOfStrings[9] = 12
# You can check the type of the element in python by printing:
print(type(listOfStrings[9])) # This will print int
print(type(listOfStrings[8])) # This will print string. As you can see there are 2 types of var in the same list (python doesn't matter)
listOfStrings[24] = False #Placing a bool in the 25th element
There is a workaround to this: you can create a function that handles the array (or use a class too, but I don't know your Python level). I'm typing a basic function for you that you can adjust for your needs
def fillArrayWithElement(array, element):
if not str(type(array)) == "<class 'list'>":
# The array is not an array
raise ValueError('The first parameter must be list type')
return
if not str(type(prova2)) == "<class 'str'>":
# The element to add is not a string
raise ValueError('The element must be str type')
return
array.append(element)
return array
python - Is there a way to set "array of strings" as a type for a parameter in a function? - Stack Overflow
How to mix strings and floats in a NumPy array?
I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.
As an aside, the way to get arrays to accept mixed types is
np.array([1, 'a'], dtype=object)
but obviously you lose practically all of the benefits of using numpy in the first place.
More on reddit.comJust a little pixelart to python array of strings converter I made this weekend, so you can display your pixelarts in the python's command line interface
How to decode a numpy array of encoded literals/strings in Python3?
Videos
You can use typing for recent python version (tested on 3.9), as suggested by @Netwave in the comments:
def my_function(a: list[str]):
# You code
Which will lint as expected:

>>> isinstance(["abc", "def", "ghi", "jkl"], list)
True
>>> isinstance(50, list)
False
You could use this inside your function in order to check if your argument is a list.
I'd like to manage headers as strings in the first column and first row of a matrix. The rest being float values.
The following trick will get the job done, but I don't think this is the right method.
array = numpy.zeros((rows,cols)).astype(object)
The idea is for conveinience when moving the data into a csv file.
Thank you for any advise!
I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.
As an aside, the way to get arrays to accept mixed types is
np.array([1, 'a'], dtype=object)
but obviously you lose practically all of the benefits of using numpy in the first place.
There is a simpler way to do this. Numpy arrays are designed for numbers. You can put whatever you want into them, but why?
Just keep a list. e.g.
array= ... headers = ["Name", "Phone", "Address", "Email"]
Write the headers, then write the array.