- List attributes Python | Basics
- How to get List attributes Python?
- Saved searches
- Use saved searches to filter your results more quickly
- Vectorized/Python-Attribute-List
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Python add/set attributes to list (Python recipe) by webby1111
List attributes Python | Basics
The list has many attributes and methods that you can use. Here are attributes of the list in Python:
- list.append(x) # append x to end of list
- list.extend(iterable) # append all elements of iterable to list
- list.insert(i, x) # insert x at index i
- list.remove(x) # remove first occurance of x from list
- list.pop([i]) # pop element at index i (defaults to end of list)
- list.clear() # delete all elements from the list
- list.index(x[, start[, end]]) # return index of element x
- list.count(x) # return number of occurances of x in list
- list.reverse() # reverse elements of list in-place (no return)
- list.sort(key=None, reverse=False) # sort list in-place
- list.copy() # return a shallow copy of the list
Note: The returned list contains the names of the methods as strings, not the methods themselves.
How to get List attributes Python?
Use the dir() function to get the List attributes in Python.
from pprint import pprint my_list = list() pprint(dir(my_list))
my_list = list() print(dir(my_list))
Another example code
my_list = [] res = dir(my_list) print(res)
Do comment if you have any doubts or suggestions on this Python List topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Add/set attributes to Python’s built-in list.
Vectorized/Python-Attribute-List
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Add/set attributes to python lists.
A google search for «add attributes to python lists» yields no good stackoverflow answer, hence the need for this.
Useful for machine learning stuff where you need labeled feature vectors.
This technique can be easily adapted for other built-ins (e.g. int).
a = [1, 2, 4, 8] a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x'
class L(list): def __new__(self, *args, **kwargs): return super(L, self).__new__(self, args, kwargs) def __init__(self, *args, **kwargs): if len(args) == 1 and hasattr(args[0], '__iter__'): list.__init__(self, args[0]) else: list.__init__(self, args) self.__dict__.update(kwargs) def __call__(self, **kwargs): self.__dict__.update(kwargs) return self a = L(1, 2, 4, 8) a.x = "Hey!" print a # [1, 2, 4, 8] print a.x # "Hey!" print len(a) # 4 # You can also do these: a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8] a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8> , x="Hey!" ) # [1, 2, 4, 8] a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8] a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ) # [2]
add or set attributes properties methods to list reopen class inheritance OOP subclass list elegant solution dictionary set monkey-patch short concise fast efficient easy simple compatible
About
Add/set attributes to Python’s built-in list.
Python add/set attributes to list (Python recipe) by webby1111
A google search for «add attributes to python lists» yields no good stackoverflow answer, hence the need for this.
Useful for machine learning stuff where you need labeled feature vectors.
This technique can be easily adapted for other built-ins (e.g. int).
The Problem
a = [1, 2, 4, 8] a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x'
The Solution
a = L(1, 2, 4, 8) a.x = "Hey!" print a # [1, 2, 4, 8] print a.x # "Hey!" print len(a) # 4 # You can also do these: a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8] a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8] a = L( , x="Hey!" ) # [1, 2, 4, 8] a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8] a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ) # [2]
Copy to clipboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
class L(list): """ A subclass of list that can accept additional attributes. Should be able to be used just like a regular list. The problem: a = [1, 2, 4, 8] a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x' The solution: a = L(1, 2, 4, 8) a.x = "Hey!" print a # [1, 2, 4, 8] print a.x # "Hey!" print len(a) # 4 You can also do these: a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8] a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8] a = L( , x="Hey!" ) # [1, 2, 4, 8] a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8] a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ) # [2] """ def __new__(self, *args, **kwargs): return super(L, self).__new__(self, args, kwargs) def __init__(self, *args, **kwargs): if len(args) == 1 and hasattr(args[0], '__iter__'): list.__init__(self, args[0]) else: list.__init__(self, args) self.__dict__.update(kwargs) def __call__(self, **kwargs): self.__dict__.update(kwargs) return self