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
Videos
In python, you wouldn't normally do what you are trying to do. But, the below code will do it:
strs = ["" for x in range(size)]
In Python, the tendency is usually that one would use a non-fixed size list (that is to say items can be appended/removed to it dynamically). If you followed this, there would be no need to allocate a fixed-size collection ahead of time and fill it in with empty values. Rather, as you get or create strings, you simply add them to the list. When it comes time to remove values, you simply remove the appropriate value from the string. I would imagine you can probably use this technique for this. For example (in Python 2.x syntax):
>>> temp_list = []
>>> print temp_list
[]
>>>
>>> temp_list.append("one")
>>> temp_list.append("two")
>>> print temp_list
['one', 'two']
>>>
>>> temp_list.append("three")
>>> print temp_list
['one', 'two', 'three']
>>>
Of course, some situations might call for something more specific. In your case, a good idea may be to use a deque. Check out the post here: Python, forcing a list to a fixed size. With this, you can create a deque which has a fixed size. If a new value is appended to the end, the first element (head of the deque) is removed and the new item is appended onto the deque. This may work for what you need, but I don't believe this is considered the "norm" for Python.