List Data Type


List is a type of compound data type which can contain ordered data of similar or different data types separated by a comma (,) and enclosed within a square brackets [ ].

List is a mutable data type means once we have assigned the size and values to a list, we can change/update them later.

In python, the operator + (plus sign) is a concatenation operator and the operator * (asterisk) is the repetition operator.

We can access the elements of a list by using the slice operator ( [ ] or [ : ] ) with index starting at 0.

Example:

    list1 = [‘Tutorials’, 123, 17.4, ‘Abhi’]
    list2 = [956, ‘Duniya’]
    print list1		            # Prints complete list
    print list1[3]		    # Prints 3th element of the list
    print list1[1:2]		    # Prints elements starting from 1st to 2nd
    print list1[1: ]		    # Prints all elements starting from 1st element
    print list2 * 3		    # Prints list 2 times
    print list1 + list2	    # Prints concatenation of list1 and list2
    list1[3] = ‘Abc’	            # Updates the 3rd element of list1 to ‘Abc’    


Output:

    [‘Tutorials’, 123, 17.4, ‘Abhi’]
    ‘Abhi’
    [123, 17.4]
    [123, 17.4, ‘Abhi’]
    [956, ‘Duniya’, 956, ‘Duniya’]
    [‘Tutorials’, 123, 17.4, ‘Abhi’, ‘Tutorials’, 123, 17.4, ‘Abhi’]
    [‘Tutorials’, 123, 17.4, ‘Abhi’, 956, ‘Duniya’]