Referring to a list

S

Sean Berry

I have a function like this:

final_list = []

def doSomething():
for item in starting_list = []:
Do all sorts of stuff... about 150 lines of processing.
Lots of conditional statements for appending to the final_list.
So I have lots of final_list.append( results ) statements.

I wanted to single out a few cases and have
them append to a different list.

The way I would "normally" do something like this would be to have
all of the .append() statements in another function like this

def addToList( listName, otherInfo )
do something with otherInfo.
then listName.append( results )

But, in this case, by the time I get to this point there
are too many variables to pass (something like 60).

So, what I want to do is use some kind of name reference
for the list I want to use.

EXAMPLE:
final_list = []
secondary_list = []

def DoSomething():
if (condition is met):
list_i_am_referring_to = final_list
else
list_i_am_referring_to = secondary_list

then I can do everything using the list_i_am_referring_to.

Is this possible???

Sorry about my poor, and lengthy explanation.
Thanks for any help.
 
R

Russell Blau

Sean Berry said:
So, what I want to do is use some kind of name reference
for the list I want to use.

EXAMPLE:
final_list = []
secondary_list = []

def DoSomething():
if (condition is met):
list_i_am_referring_to = final_list
else
list_i_am_referring_to = secondary_list

then I can do everything using the list_i_am_referring_to.

Is this possible???

Does this help?
odd_list = []
even_list = []
def which_list(x):
if x % 2 == 0:
return even_list
else:
return odd_list
which_list(i).append(i)

odd_list [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
even_list
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
 
L

Larry Bates

Absolutely. Everything in Python is a pointer.

x=final_list

is a pointer to final_list

x=final_list_other

changes what x points to.

Larry Bates
Syscon, Inc.
 
D

Dave Kuhlman

Russell Blau wrote:

[snip]
Does this help?
odd_list = []
even_list = []
def which_list(x):
if x % 2 == 0:
return even_list
else:
return odd_list
for i in range(20):
which_list(i).append(i)

odd_list [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
even_list
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Basically, what this and other replies to your question are
telling you is that lists in Python are first-class objects. To
be a first-class object means that you can (1) stuff it into a
data structure (for example, a dictionary or another list), pass
it in to a function, and (3) return it as the value of a function.

Also remember that functions in Python are first class, so you do
something like this:

def do_job(arg, listarg, even_func, odd_func):
if arg % 2 == 0:
even_func(arg, listarg)
else:
odd_func(arg, listarg)

That makes doing delegation in Python easy and natural. OK. OK.
It's not natural, but it's about as natural as something as weird
as computer programming is going to get.

Dave
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top