python list

By: Ashley J Viewed: 181 times  Printer Friendly Format    


A Python list is a mutable ordered sequence of items. The items of a list are arbitrary objects and may be of different types. To denote a list, use a series of expressions (the items of the list) separated by commas (,), within square brackets ([]); if every item is a literal, the whole assembly is a list literal. You may optionally place a redundant comma after the last item. To denote an empty list, use an empty pair of brackets. Here are some example list literals:

[42, 3.14, 'hello']        # List with three items
[100]                      # List with one item
[]                         # Empty list

You can also call the built-in type list to create a list. For example:

list('wow')
This builds a list equal to that denoted by the list literal:

['w', 'o', 'w']
list() without arguments creates and returns an empty list, like []. When x is iterable, list(x) creates and returns a new list whose items are the same as those x.

MODIFYING A LIST

You can modify a single item in a list by assigning to an indexing. For instance:

x = [1, 2, 3, 4]
x[1] = 42                # x is now [1, 42, 3, 4]


You can also modify a range of elements in a list as below. For example:

x = [1, 2, 3, 4]
x[1:3] = [22, 33, 44]     # x is now [1, 22, 33, 44, 4]
x[1:4] = [8, 9]           # x is now [1, 8, 9, 4]

SORTING A LIST

A list’s method sort() causes the list to be sorted in-place (reordering items to place them in increasing order).

mylist = ['alpha', 'Beta', 'GAMMA']
mylist.sort()                  #  ['Beta', 'GAMMA', 'alpha']
mylist.sort(key=str.lower)     #  ['alpha', 'Beta', 'GAMMA']

Python also provides the built-in function sorted() to produce a sorted list from any input iterable. sorted, after the first argument (which is the iterable supplying the items), accepts the same arguments as a list’s method sort.

Other List Operations

Method Description

Nonmutating

 

L .count( x )

Returns the number of items of L that are equal to x.

L .index( x )

Returns the index of the first occurrence of an item in L that is equal to x, or raises an exception if L has no such item.

Mutating

 

L .append( x )

Appends item x to the end of L ; like L[len(L):]=[x].

L .extend( s )

Appends all the items of iterable s to the end of L ; like L[len(L):]=s or L += s.

L.insert(i, x)

Inserts item x in L before the item at index i, moving following items of L (if any) “rightward” to make space (increases len(L) by one, does not replace any item, does not raise exceptions; acts just like L[i:i]=[x]).

L .remove( x )

Removes from L the first occurrence of an item in L that is equal to x, or raises an exception if L has no such item.

L.pop(i=-1)

Returns the value of the item at index i and removes it from L; when you omit i, removes and returns the last item; raises an exception if L is empty or i is an invalid index in L.  

L .reverse()

Reverses, in place, the items of L.

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

Sorts, in-place, the items of L, comparing items pairwise via—v2, only —the function passed as cmp (by default, the built-in function cmp). When argument key is not None, what gets compared for each item x is key(x), not x itself.



Most Viewed Articles (in Python )

Latest Articles (in Python)

Comment on this tutorial