Lists in Python

 What is List?

List are used to store multiple items in a single variable, which can be written as a list of commas separated values using square brackets "[]". Unlike Strings, Lists are mutable that means elements inside the list can be changes/updated/modified. 


How to create list?

first_list=[4,5,6]
second_list=[4,'five',6]   ---- list can hold different types of objects, here we have used integer & string 


Example:

>>> first_list=[4,5,6]
>>> first_list
[4, 5, 6]
>>> second_list=[4,'five',6]
>>> second_list
[4, 'five', 6]


Can we use indexing and Slicing?

Yes, Indexing and Slicing work just like in String!

Create a list:

third_list=[4,'five',6,'seven','eight',9]


Example:

>>> third_list=[4,'five',6,'seven','eight',9]
>>> third_list[1]    ------------- Indexing method
'five'

>>> third_list[3:]  ------------ Indexing and Slicing method
['seven', 'eight', 9]


Common List Methods

They are various methods available however, have listed few of them those are popular.

  • append
  • pop
  • reverse
  • sort 

append: 

Use to add an item to the end of the list (permanently, and by default it adds an item to the end of the list

Example:
>>> list1=['a','z','q','w']
>>> list1.append('b')
>>> list1
['a', 'z', 'q', 'w', 'b']   ---------- "b" is appended 

pop: 

Use to delete an item from the list (by default it delete the item from the last index i.e, from the reverse index poistion "[-1]"). However, we can define the specific index position.

Example:
>>> list1=['a','z','q','w']
>>> list1.pop()
'w'                          -------------- resulted the deleted item (using the reverse index postion "[-1]"
>>> list1
['a', 'z', 'q']            ------------- "w" is deleted from the list


Here, we will be using specific index position to delete the item from the list

>>> list1=['a','z','q','w']
>>> list1.pop(1)    ------------- used the specific index position "1" (it should delete 'z')
'z'                            ------------- It is deleted the element from index position "1" 
>>> list1
['a', 'q', 'w']

reverse: 

Use to reverse the order of the list (Permanently)

Example:

>>> list1=['a','z','q','w']
>>> list1.reverse()
>>> list1
['w', 'q', 'z', 'a']

You can't directly store reverse result into any variable (don't do this mistake), let see the example

>>> list1=['a','z','q','w']
>>> list1_result=list1.reverse()
>>> list1_result             ----------------- resulted nothing/None
>>> print(list1_result)
None                               ----------------- observer here, the result is "None"  because it keep the result in  
                                                               place

you can try validating the type,

>>> type(list1_result)
<class 'NoneType'>        ----------------- resulted the type as "NoneType"


Note: "NoneType" is the type of object in Python, and which indicates that no value. None is the return value of functions that "don't return anything"


So how to fix this issue, let see an example,

>>> list1=['a','z','q','w']
>>> list1.reverse()           -------- revers it
>>> list1_result=list1     -------- store the reverse value into a variable
>>> print(list1_result)    -------- print it
['w', 'q', 'z', 'a']


sort: 

Use to sort the list (for numbers: it will use ascending and for string it go with alphabetical order). Additionally you can't store the sort result directly into any variable.

>>> list1=['a','z','q','w']
>>> list1.sort()
>>> list1
['a', 'q', 'w', 'z']


>>> list1=['a','z','q','w']
>>> list1.sort()
>>> final_list1=list1
>>> final_list1
['a', 'q', 'w', 'z']


Nested List

A Nested list is a list of lists, that means a list that has another list of elements inside in it called a sublist. It is a great feature of Python data structure that support nesting. This means we can have data structure within data structures. 

Example:

my_nest_list=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]


For example,

>>> list1=[1,2,3,4]
>>> list2=[5,6,4,8]
>>> list3=[9,10,11,12]
>>> my_new_list=[list1,list2,list3]
>>> my_new_list
[[1, 2, 3, 4], [5, 6, 4, 8], [9, 10, 11, 12]]

Here is the example to use indexing on nesting list:

>>> my_new_list
[[1, 2, 3, 4], [5, 6, 4, 8], [9, 10, 11, 12]]
>>> my_new_list[1]            ---------------- indexing position of 1 
[5, 6, 4, 8]
>>> my_new_list[1][2]       ---------------- index position of 1 nesting index position of 2
4


Comments

Popular posts from this blog

Auditing in Oracle database

rs.stepDown() in MongoDB replication

Tuples in Python