Serdar Eren Serdar Eren - 23 days ago 9
Python Question

Odoo 10 python array list call element

@api.depends('employee_id')
def create_employee_report(self):
count = 0
employee_array = []
employee_data = {}
for employee in self.env['hr.employee'].search([]):
if employee.socialsecurityno:
count = count + 1
employee_data ={'count':str(count),'sicil_no':employee.socialsecurityno}
employee_array.append(employee_data)
print employee_array
print employee_array[0][count]


How can i call the element?

employee_array[0].count


or

employee_array[0[count]]


i tried all combinations but it is not working.
Thank you.

Answer Source

You should use 'print employee_array[0]["count"]' as 'count' is a key of your dictionary. If you don't keep it in quotes python assumes that it is a variable. And I assume you always want to print the 0th element from your array as per your code.

An example snippet of what is happening inside for loop:

>>> employee_array = []
>>> employee_data ={'count':"1",'sicil_no':"9704"}
>>> employee_array.append(employee_data)
>>> print employee_array
[{'count': '1', 'sicil_no': '9704'}]
>>> print employee_array[0]["count"]
1
>>> employee_data ={'count':"2",'sicil_no':"9705"}
>>> employee_array.append(employee_data)
>>> print employee_array
[{'count': '1', 'sicil_no': '9704'}, {'count': '2', 'sicil_no': '9705'}]
>>> print employee_array[0]["count"]
1

OR may be you are looking for:

>>> print employee_array[count]

and not:

>>> print employee_array[0]["count"]