List Vs Tuple in python with Odoo

Hello everyone

In this article we will emphasize the difference in the use of tuple and list :

list gives you values between brackets “[]”. For example, when we do in odoo:

account_ids = self.env['account.account'].search([])
account_ids ---> [1, 4, 5, 8 , 9]

account_ids is a list of values

We can use this list:

* in loops:

for account_id in account_ids:
     move = self.env['account.account'].search([('account_id', '=', account_id)])

* to search in another table:

move = self.env['account.account'].search([('account_id', 'in', account_ids)])

* to change or search a specific value:

account_ids[2] -----> 5
account_ids[4] -----> 9
account_ids[0] = 10 -----> will replace the first value "1" by "10"

* change a tuple to a list:

 list(my_tuple)

tuple gives you values between parentheses “()”. we can use them in :

* change a tuple to a list:

account_ids = tuple(self.env['account.account'].search([])) 
account_ids ---> (1, 4, 5, 8 , 9)

* in loops:

for account_id in account_ids:
     move = self.env['account.account'].search([('account_id', '=', account_id)])

* to search in another table:

move = self.env['account.account'].search([('account_id', 'in', account_ids)])

* in SQL filter Query:

In SQL Query we should use a tuple of values and not a list

account_ids = tuple(self.env['account.account'].search([]))
cr.execute('SELECT * FROM account_move_line where account_id in %s,(account_ids))