PYTHON LIST

List is a data structure.We can called it as sequence or container which store the items of different types inside a square bracket [ ] by a comma (,).  List is ordered and mutable (changeable). List can have duplicate items. 

Define List

defining a list

animals = ['deer','tiger','lion']    #define a list

'animals' is a name of the list with items 'deer', 'tiger' and 'lion'

Print a List

print(list) will print the values of the list

animals = ['deer','tiger','lion']
print(animals)

 output : ['deer', 'tiger', 'lion'] 

length of list

len() is a function which print the length (number of items) of the list

animals = ['deer','tiger','lion']
print(len(animals))

Output: 3

Index of list

index:  index is a number to specify the position of element in the list. With the help of index, we can print the item. If n items are there, 0 to n-1 are the index positions. 

In animals list 3 items ( 'deer','tiger','lion')  are there, 0, 1,2 are the respective index positions. 

    animals = ['deer','tiger','lion']
    print(animals[0])
    print(animals[1])
    print(animals[2])

    Output: deer
        tiger
        lion

We can find the index of particular item in the list if it exists, if item is repeated it will give first occurrence.

    animals = ['deer','tiger','lion','deer','bear','tiger']
    print(animals.index('tiger'))

    Output: 1

Negative Index: 

Negative index will start from the last position. 

animals = ['deer','tiger','lion']

                   -3      -2      -1        are the negative index positions 

Append Method

append will add element at last to the list

    animals = ['deer','tiger','lion']
    animals.append('bear')
    print(animals)

 Output: ['deer', 'tiger', 'lion', 'bear']

Insert Method

insert will insert the element at specified position

    animals = ['deer','tiger','lion']
    animals.insert(2,'cat')
    print(animals)

Output: ['deer', 'tiger', 'cat', 'lion']

Here, we have 3 items 0,1,2 are index positions. If we try to insert at these index positions (0,1,2) the items will move to right from that position. lion is at 2nd position but when we try to insert an element (cat) it moved lion right side. 

    If we try to insert element other than 0,1,2 position, the element will add at last. It will not throw an error. 

    animals = ['deer','tiger','lion']
    animals.insert(5,'cat')
    print(animals)

Output: ['deer', 'tiger', 'lion', 'cat']

But it will throw an IndexError if we try to access an element which is not in that position. We added 'cat' at 5th position but 'cat' added at last which 3rd position. 

    animals = ['deer','tiger','lion']
    animals.insert(5,'cat')
    print(animals[5])

IndexError: list index out of range

Remove Method

remove will remove a specified element from the list

    animals = ['deer','tiger','lion']
    animals.remove('tiger')
    print(animals)

Output: ['deer', 'lion']

Pop Method

pop will remove the last item from the list, if we don’t specify the index. 

    animals = ['deer','tiger','lion']
    animals.pop()
    print(animals)

Output: ['deer', 'tiger']

If we specify the index it will remove item at that index position. 

    animals = ['deer','tiger','lion']
    animals.pop(1)
    print(animals)

    Output: ['deer', 'lion']

Reverse Method

Reverse method will reverse the list

    animals = ['deer','tiger','lion']
    animals.reverse()
    print(animals)

Output: ['lion', 'tiger', 'deer']

Sort Method

sort will sort the list in ascending or descending order. By default, it is ascending order. If we want, we can specify descending order. Remember to sort a list, the list must contain elements of same data type. 

    animals = ['deer','tiger','lion']
    animals.sort()
    print(animals)
    animals.sort(reverse=True)
    print(animals)

    Output:['deer', 'lion', 'tiger']     ascending
               ['tiger', 'lion', 'deer']     descending

Count

count will give the how many times the item is there in the list

    animals = ['deer','tiger','lion','deer','tiger','deer']
    print(animals.count('deer'))

Output: 3

Clear

clear will clear all elements in the list.  

    animals = ['deer','tiger','lion','deer','tiger','deer']
    animals.clear()
    print(animals)

Output: [ ]                 Empty List Clear method removes all elements

Copy

copy will copy the list into another variable 

    animals = ['deer','tiger','lion']
    ani = animals.copy()
    print(ani)

Output: ['deer', 'tiger', 'lion']

Extend

extend will add another list to the existing list 

    animals = ['deer','tiger','lion']
    animals1 = ['lion', 'cat', 'cow']
    animals.extend(animals1)
    print(animals)

Output: ['deer', 'tiger', 'lion', 'lion', 'cat', 'cow']

List concatenation

We add two lists with the help of + operator. Here we are adding the 'animals1' list and 'animals2' list into 'animals'. We can add a list with list only

    animals1 = ['deer','tiger','lion']
    animals2 = ['lion', 'cow']
    animals = animals1+animals2
    print(animals)

Output: ['deer', 'tiger', 'lion', 'lion', 'cow']

We can repeat the list also. We are repeating fruits list 2 times 

    animals = ['deer','tiger','lion']
    print(animals*2)

Output: ['deer', 'tiger', 'lion', 'deer', 'tiger', 'lion']

List Slicing

    Slicing the list is nothing but creating sub list from an existing list. 

    Syntax for slicing the list. 

    list_name[start:stop:step] 

    Default values of a start is 0 (0 index position), stop last position, step is 1. 

        animals = ['deer','tiger','lion','fox','hippo']
        print(animals[::])

Output: ['deer', 'tiger', 'lion', 'fox', 'hippo']

Printing list from a specified position to last 

        animals = ['deer','tiger','lion','fox','hippo']
        print(animals[2:])

Output: ['lion', 'fox', 'hippo']

Printing list from starting to specified position (last index position not included)

        animals = ['deer','tiger','lion','fox','hippo']
        print(animals[:3])

Output: ['deer', 'tiger', 'lion']

Printing list from index position 1 to 5 (5th index excluded) 

        animals = ['deer','tiger','lion','fox','hippo','tiger']
        print(animals[1:5])

Output: ['tiger', 'lion', 'fox', 'hippo']

start at 1st index, stop at 5th index and step up 2.So it will take 'tiger' at 1st index, then step 2 positions up at 3rd index position it will take 'fox', then 5th index position (but it will not include last position).

        animals = ['deer','tiger','lion','fox','hippo','tiger']
        print(animals[1:5:2])

Output: ['tiger', 'fox']

Reverse a List with Slicing

We can reverse a list with help of slicing methodology

        animals = ['deer','tiger','lion','fox','hippo','tiger']
        print(animals[::-1])

Output: ['tiger', 'hippo', 'fox', 'lion', 'tiger', 'deer']

List Comprehension

List comprehension is an elegant way of defining the list.

        numbers = [x**3 for x in range(1,6)] 
        print(numbers) 

Output:[1, 8, 27, 64, 125] 

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform