How to declare and add items to an array in Python?


I’m trying to add items to an array in python.

I run

array = {}

Then, I try to add something to this array by doing:


There doesn’t seem to be a .append method for this. How do I add items to an array?

Asked By: AkshaiShah



If you do it this way:

array = {}

you are making a dictionary, not an array.

If you need an array (which is called a list in python ) you declare it like this:

array = []

Then you can add items like this:

Answered By: Pablo Santa Cruz

{} represents an empty dictionary, not an array/list. For lists or arrays, you need [].

To initialize an empty list do this:

my_list = []


my_list = list()

To add elements to the list, use append


To extend the list to include the elements from another list use extend

--> [12,1,2,3,4]

To remove an element from a list use remove


Dictionaries represent a collection of key/value pairs also known as an associative array or a map.

To initialize an empty dictionary use {} or dict()

Dictionaries have keys and values

my_dict = {'key':'value', 'another_key' : 0}

To extend a dictionary with the contents of another dictionary you may use the update method

my_dict.update({'third_key' : 1})

To remove a value from a dictionary

del my_dict['key']
Answered By: lukecampbell

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 = []
Answered By: Collin Green

In some languages like JAVA you define an array using curly braces as following but in python it has a different meaning:


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:


mylist = [1,2,3]

or other examples like:

mylist = list()
>>> [1,2,3]
Answered By: user1767754

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.

Answered By: Eliza

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'
Answered By: Big McLargeHuge

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 = ...

Answered By: Aurelie Giraud

Isn’t it a good idea to learn how to create an array in the most performant way?

It’s really simple to create and insert an values into an array:

my_array = ["B","C","D","E","F"]

But, now we have two ways to insert one more value into this array:

Slow mode:

my_array.insert(0,"A") – moves all values ​​to the right when entering an "A" in the zero position:

"A" --> "B","C","D","E","F"

Fast mode:


Adds the value "A" to the last position of the array, without touching the other positions:

"B","C","D","E","F", "A"

If you need to display the sorted data, do so later when necessary. Use the way that is most useful to you, but it is interesting to understand the performance of each method.

Answered By: Fellipe Sanches
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.