Lists

walden systems, geeks corner, lists, python, tuples, strings, languages, programming
Python is a programming language that lets you work quickly and integrate systems more effectively.

The most basic data structure in Python is the sequence. Each element of a sequence is indexed by a number. The first index is zero, the second index is one, and so forth. Python has six built-in types of sequences, but the most common ones are lists and tuples.

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.


Lists

The list is the most versatile datatype available in Python. It can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type. Creating a list consists of putting different comma-separated values between square brackets. As with string indices, list indices start at 0, and lists can be sliced, concatenated and so on.

    list1 = [1, 2, 3, 4, 5 ];
    list2 = ["a", "b", "c", "d"]

Accessing, updating, adding and deleting elements in a list

To access values in lists, use the square brackets with the index or indices to obtain the value or values available at that position. You can update single or multiple elements of a list by giving the index or range of indices on the left-hand side of the assignment operator.

To add an element to a list, use append() method which will add the element to the end of a list. To add multiple elements to a list, use the extend() method.

To delete an item, there are two methods available, remove() and del(). If you know the index you want to delete, use the del() method. If you don't know the index but know the value of the object, use the remove() method which will remove the first matching element in the list. If you just want to remove an element and you want the return the element, use the pop() method. With the pop() method, if you don't provide an index, the last element in the list will be deleted and returned.

Accessing an element in a list

    list = ["a", "b", "c", "d"]
    
    foo = list[ 1 ]  
    # foo will get the value "b"    


Accessing multiple elements of a list
    list = ["a", "b", "c", "d"]

    list2 = list[ 1: 3 ]
    # list2 will have the values [ "b", "c", "d" ]


Updating a single element in a list
    list = ["a", "b", "c", "d"]

    list[ 1 ] = "Z"
    # list will now contain ["a", "Z", "c", "d"]


Updating multiple elements in a list
    list = ["a", "b", "c", "d"]

    list [ 2 : 3 ] = [ "y", "z" ]
    # list will now contain ["a", "b", "y", "z"]


Adding an element to a list
    list = ["a", "b", "c", "d"]

    list.append( "e" )
    # list will now contain ["a", "b", "c", "d", "e"]


Deleting an item from a list
    list = ["a", "b", "c", "d"]

    del list[ 1 ] 
    # list will contain ["a", "c", "d"]


Deleting multiple elements from a list
    list = ["a", "b", "c", "d"]

    del list[ 1: 2 ]
    # list will contain ["a", "d"]


Delete the last element from a list
    list = ["a", "b", "c", "d"]

    list.remove( "b" )
    # list will contain = ["a", "c", "d"]


Delete an element in a list and return the element
    list = ["a", "b", "c", "d"]
    
    foo = list.pop( 1 )
    # list will contain = ["a", "c", "d"]
    # the value of foo will be "b"


Delete the last element in the list and return the element
    list = ["a", "b", "c", "d"]

    foo = list.pop( )
    # list will contain ["a", "b", "c"]
    # the value of foo will be "d"

List operators

Lists has the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string. Lists respond to all of the general sequence operations used on strings.

Length

    len(["a", "b", "c"])
    # returns 3


Concatenate
    ["a", "b", "c"] + ["d". "e", "f"]
    # result is ["a", "b", "c", "d", "e", "g"]
Repetition
    ["hello"] * 3
    # result is ["hello", "hello"]


Membership
    "b" in ["a", "b", "c"]
    # returns true since b exists in the list


Iteration
    for x in ["a", "b", "c"] :
        print x
    # will output abc to the screen