Python 3-Lists

Python List

List is collection of different objects which are ordered and it is mutable. Since the declaration of all values follows the same syntax “var = value”, based on the value representation the type of the var is determined like other variable declaration.

Creation of List data:

To create a list data type we can just give an empty square braces to a var
>>> Var = [] # creates an empty list objects and referred to Var.
>>> Var = list() # this also creates the same empty list
>>> Var = [1,2,'apple','baby'] #To create a list with static values, we have to give comma separated objects within square braces.
The same way we can pass an iterable to list function call
>>> Var = list("iterable") # -> [‘i’,’t’,’e’,’r’,’a’,’b’,’l’,’e’]. 

Adding elements into a list object:

To add the element to an existing object, we have two methods to perform adding the elements to a list:

  • Append
  • Insert

Append -> used to add the object to the last index of the list object

obj.append(value)

Insert-> used to add the object at the specific index of the list object

obj.insert(index, value)

Example:

>>> a=['apple','banana']
>>> a.append('strawberry')
>>> a.append('mango')
>>> print(a)
['apple', 'banana', 'strawberry', 'mango']
>>> a.insert(1,'blueberry')
>>> print(a)
['apple', 'blueberry', 'banana', 'strawberry', 'mango']
>>>>>> a.insert(100,'cranberry')
>>> print(a)
['apple', 'blueberry', 'banana', 'strawberry', 'mango', 'cranberry']
>>>

Accessing any values from list object:

Through indexing we can access any object from the list. For example,

Syntax-> obj[index]

In Python, we can do both forward and reverse indexing. Forward indexing starts from 0 to len(obj)-1.

Reverse indexing starts from -1 to last index.

>>> print(a)
['apple', 'blueberry', 'banana', 'strawberry', 'mango', 'cranberry']
>>> print(a[0])
apple
>>> print(a[1])
blueberry
>>> print(a[-1])
cranberry
>>> print(a[-3])
strawberry
>>>
>>> for i in a:
...     print(i, end =' ')
...
apple blueberry banana strawberry mango cranberry
>>>

Modify a value from a list:

To modify a value from a list, we need to refer an index value and assign a new value to it.

Syntax -> obj[index] = new_value

>>> print(a)
['apple', 'blueberry', 'banana', 'strawberry', 'mango', 'cranberry']
>>> a[2] = 'fruits'
>>> print(a)
['apple', 'blueberry', 'fruits', 'strawberry', 'mango', 'cranberry']
>>>

Remove elements from a List:

There are three ways to delete any element from the list. They are:

  • Del -> del obj[index] #delete specific index value from the list
  • Pop -> obj.pop(index) #remove values based on the index value of the list 
  • Remove -> obj.remove(value) value based removal
>>> print(a)
['apple', 'blueberry', 'fruits', 'strawberry', 'mango', 'cranberry']
>>> del a[2]
>>> print(a)
['apple', 'blueberry', 'strawberry', 'mango', 'cranberry']
>>> a.pop()
'cranberry'
>>> print(a)
['apple', 'blueberry', 'strawberry', 'mango']
>>> a.pop(2)
'strawberry'
>>>
>>> print(a)
['apple', 'blueberry', 'mango']
>>> a.remove('apple')
>>> print(a)
['blueberry', 'mango']
>>> a.clear() #used to delete all the values in list and makes it empty
>>> a
[]
>>>

Merge two list into one:

To merge two list into one we can use built-in extend method in list objects.

Syntax -> obj.extend(another_list)

>>> x=[1,2,3,4]
>>> x
[1, 2, 3, 4]
>>> y=[6,8,9]
>>> y
[6, 8, 9]
>>> x.extend(y)
>>> x
[1, 2, 3, 4, 6, 8, 9]
>>>