Posts Tagged ‘Array’

How to loop over an array in Python?

You can use a for in loop to get the elements of the array. colours = ["red","green","blue"]   for colour in colours: print colour   # red # green # blue However if you also need access to the position of the element in the array we can use a for in loop over a [...]

How to create an associative array in Python?

Associative arrays in Python are called dictionaries. # Dictionary with quoted or variable keys d1 = { "name":"donuts", "type":"chocolate", "quantity":10 }   # Dictionary with fixed keys d2 = dict(name="donuts", type="chocolate", quantity=10)   assert d1 == d2