Can I append a list obtained from a Class as output outside the class , if Yes how ?? can i use userList for this purpose ?

Recommended Answers

All 4 Replies

Can you elaborate on this, your question isn't clear to me.

You mean something like this ...

'''class_customlist1.py
build your own custom list with a class inheriting Python's list object
'''

class CList(list):
    '''inherits list object'''
    def __init__(self, alist=[]):
        self.alist = alist

    def append(self, item):
        return self.alist.append(item)

    def show(self):
        print(self.alist)


clist = CList(['x', 'y'])

# you can use
clist.append('a')
# or
clist.alist.append('b')

# same as print(clist.alist) 
clist.show()

print('-'*20)

for item in clist.alist:
    print(item)

'''result ...
['x', 'y', 'a', 'b']
--------------------
x
y
a
b
'''

Yes this helps a lot but I guess I require on more thing that is if i can create list witnin list by adding a method here... like clist = [] an empty list , alist = [1,2,3], blist = [4,5,6]

and the result is clist = [ [1,2,3], [4,5,6] ..] Is this possible to do in python with lists ??

Certainly you can nest lists inside another list, and you wouldn't necessarily need another method to do it. If you already have the list to insert, the it is as easy as

>>> alist = [1,2,3]
>>> blist = [4,5,6]
>>> clist = []
>>> clist.append(alist)
>>> clist.append(blist)
>>> clist
[[1, 2, 3], [4, 5, 6]]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.