0% found this document useful (0 votes)
26 views3 pages

List - Append A (Len (A) :) (X) List - Extend: Sorted

The list data type in Python has several useful methods for manipulating list elements and data. These methods allow users to append, insert, remove, pop, count, sort, and reverse elements in a list. Common list methods include append(), insert(), remove(), pop(), count(), sort(), and reverse().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

List - Append A (Len (A) :) (X) List - Extend: Sorted

The list data type in Python has several useful methods for manipulating list elements and data. These methods allow users to append, insert, remove, pop, count, sort, and reverse elements in a list. Common list methods include append(), insert(), remove(), pop(), count(), sort(), and reverse().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LIST

The list data type has some more methods. Here are all of the methods of list objects:

list.append(x)

Add an item to the end of the list; equivalent to a[len(a):] = [x].

list.extend(L)

Extend the list by appending all the items in the given list; equivalent to
a[len(a):] = L.

list.insert(i, x)

Insert an item at a given position. The first argument is the index of the element
before which to insert, so a.insert(0, x) inserts at the front of the list, and
a.insert(len(a), x) is equivalent to a.append(x).

list.remove(x)

Remove the first item from the list whose value is x. It is an error if there is no such
item.

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is
specified, a.pop() removes and returns the last item in the list. (The square
brackets around the i in the method signature denote that the parameter is optional,
not that you should type square brackets at that position. You will see this notation
frequently in the Python Library Reference.)

list.index(x)

Return the index in the list of the first item whose value is x. It is an error if there is
no such item.

list.count(x)

Return the number of times x appears in the list.

list.sort(cmp=None, key=None, reverse=False)

Sort the items of the list in place (the arguments can be used for sort customization,
see sorted() for their explanation).
list.reverse()

Reverse the elements of the list, in place.

An example that uses most of the list methods:

a = [66.25, 333, 333, 1, 1234.5]


print a.count(333), a.count(66.25), a.count('x')
a.insert(2, -1)
print a
a.append(333)
print a
print a.index(333)
a.remove(333)
print a
a.reverse()
print a
a.sort()
print a
print a.pop()
print a

You might also like