Arrays (called list
in python) use the []
notation. {}
is for dict
(also called hash tables, associated arrays, etc in other languages) so you won't have 'append' for a dict.
If you actually want an array (list), use:
array = []
array.append(valueToBeInserted)
I believe you are all wrong. you need to do:
array = array[]
in order to define it, and then:
array.append ["hello"]
to add to it.
Just for sake of completion, you can also do this:
array = []
array += [valueToBeInserted]
If it's a list of strings, this will also work:
array += 'string'
You can also do:
array = numpy.append(array, value)
Note that the numpy.append()
method returns a new object, so if you want to modify your initial array, you have to write: array = ...
In some languages like JAVA you define an array using curly braces as following but in python it has a different meaning:
Java:
int[] myIntArray = {1,2,3};
String[] myStringArray = {"a","b","c"};
However, in Python, curly braces are used to define dictionaries, which needs a key:value
assignment as {'a':1, 'b':2}
To actually define an array (which is actually called list in python) you can do:
Python:
mylist = [1,2,3]
or other examples like:
mylist = list()
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist)
>>> [1,2,3]
No, if you do:
array = {}
IN your example you are using array
as a dictionary, not an array. If you need an array, in Python you use lists:
array = []
Then, to add items you do:
array.append('a')
Source: Stackoverflow.com