While working with lists sometimes I got "list object has no attribute" type of results.So I think it's better to know which are the in-built operations we can perform on list.
Asked
Active
Viewed 80 times
-3
-
1Check out the python [documentation](https://docs.python.org/3/tutorial/datastructures.html) for list. – Paul Rooney Aug 14 '18 at 04:56
-
3`dir(list)` will enumerate all the methods available on `list` objects – Reblochon Masque Aug 14 '18 at 04:57
-
3The canonical way: reading [the documentation](https://docs.python.org/3/tutorial/datastructures.html). You can also directly inspect the object's documentation from the REPL: `help(list)`. – Amadan Aug 14 '18 at 04:57
-
Possible duplicate of [Finding what methods a Python object has](https://stackoverflow.com/questions/34439/finding-what-methods-a-python-object-has) – Reblochon Masque Aug 14 '18 at 06:00
2 Answers
2
There are two primary ways to do this.
Use
dir()
to list all the attributes related to an object.attempt to return a list of valid attributes for that object
>>> dir(list) => ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Use
help()
to get a consorted list of all the related functions, attributes etc.Invoke the built-in help system
>>> help([]) Help on list object: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. ... and so on

Kaushik NP
- 6,733
- 9
- 31
- 60
1
You can run dir()
on your list object to get the attributes.
dir([])
Will show:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

jh314
- 27,144
- 16
- 62
- 82