Skip to content

List attributes Python | Basics

  • by

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))

OR

my_list = list()

print(dir(my_list))

Output:

List attributes Python

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading