List Order of Initialization

S

SamFeltus

When a list initializes, will it always evaluate in order starting at
element 0 and finishing with the last element?

def f1(x):
return x + 2

def f2(x):
return x * 2

def f3(x):
return x * 3

the_list = [f1(7), f2(8), f3(4)]
 
D

Diez B. Roggisch

SamFeltus said:
When a list initializes, will it always evaluate in order starting at
element 0 and finishing with the last element?

def f1(x):
return x + 2

def f2(x):
return x * 2

def f3(x):
return x * 3

the_list = [f1(7), f2(8), f3(4)]

Yes.

Diez
 
T

Terry Reedy

Diez said:
SamFeltus said:
When a list initializes, will it always evaluate in order starting at
element 0 and finishing with the last element?

def f1(x):
return x + 2

def f2(x):
return x * 2

def f3(x):
return x * 3

the_list = [f1(7), f2(8), f3(4)]

Yes.

From the fine manual:

Python evaluates expressions from left to right. Notice that while
evaluating an assignment, the right-hand side is evaluated before the
left-hand side.

In the following lines, expressions will be evaluated in the arithmetic
order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top