Create array in Python

Posted under » Python » Django on 24 May 2022

Initialise

cars=[]

Use the len() method to return the length of an array (the number of elements in an array).

x = len(cars)

Looping

cars = ["Ford", "Volvo", "BMW"]

for x in cars:
  print(x)

You can use the append() method to add an element to an array.

cars.append("Honda") 

Assuming you have a car associative array and wants just the model id (m_id field) sorted by car type in django

 model_list = []
 for mod in carass:
     model_list.append(mod.m_id)
 # use the model_list array
 carmod = cars.objects.filter(model__in=model_list).order_by('type')

Note that carass and cars are 2 separate tables or objects. The '__in' means the model is equal to the ids on the model_list array.

Removing Array Elements by pop or remove

cars.pop(1) #2nd element

cars.remove("Volvo")

For PHP array.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS