Hi. I was wondering if theres a way to take a specific item from a list to another list.

Here's what I thought would work...:

def __init__(self, indata):

        self.data = indata
        self.total = len(self.data)

    def getTtestFactorA(self, factorA):

        ListFactorA = []

        for factorA in self.data:
            ListFactorA.append()
        return ListFactorA

    def getTtestFactorB(self, factorB):

        ListFactorB = []
        
        for factorB in self.data:
            ListFactorB.append()
        return ListFactorB

The variables factorA and factorB are given by the user in another class.

So if the user chose factorA as 6, python would add the 6th element in the list self.data to ListFactorA.

Thanks in advance.

Edited 5 Years Ago by kpxny2sk: n/a

6th item of the list is item number 5, so you must reduce factorA to accomodate this 0-based indexing. __init__ should have same indentation as other def's, probably cut-and-paste error.

def __init__(self, indata):

        self.data = indata
        self.total = len(self.data)

    def getTtestFactorA(self, factorA):

        ListFactorA = []

        for factorA in self.data:
            ListFactorA.append(self.data[factorA-1])
        return ListFactorA

It would be worth consideration to learn to use zero based indexing and call 6th elementh 5th, it is not so strange as you think (but user is allways right and the programmer's task mostly is to do the way users prefer, even if it is not necessary best way or most logical way). That would reduce risk of confusion from two one-off interpretations of indexing. You might consider conforming to Python style naming if you do not need to confirm to company naming rules, for example capitalized name should name that variable refers to object, not basic data type like list and parts of name are joined by _. Generally also it is good idea to use plural names for sequenses and leave out list or such (it is implementation detail you might change later). So for example I would call ListFactorB as factor_bs.

But actually you should not use this kind of accessor functions for objects in Python, but you would add __index__ method to get object's nth data.

def __index__(self,n):
         return self.data[n-1]

And if you want to prepare list of one element you do not use append but:

return [self.data[factorA-1]]

Edited 5 Years Ago by pyTony: n/a

Yep. I understand that the first element would be 0, but my question was how to actually code it so that python adds factorA'th element from the list 'self.data' into another list 'ListFactorA'

Thanks.

ListFactorA in your code is local variable and it is only returned out of function, it suffice to return the one element list directly. If you want to modify existing list in function you should pass the list as parameter to function and modify it. Then it is not necessary to return the list as the original list will be modified (procedure style instead of function style function).

This article has been dead for over six months. Start a new discussion instead.