Skip to main content

Python 3 Part 3 - Understanding list data structure

 List


 

Table of content 

  1. List
  2. How to define list
  3. Access list elements
  4. Modify a list element
  5. Nested List
  6. List functions

List

It is inbuilt data structure which can store heterogeneous data elements.

How to define list

To define list we use [] (Square Brackets).

Access list elements

We can access the data elements within list with index. In the above example list with index will be.


To access the 0th element we index the list as ls[0], likewise to access 1st element,  ls[1] and 2st element ls[2].


 Modify a list element

Note: It is mutable data structure, means we can do changes in the data elements.
Suppose we want to replace 0th element i.e 1 with 100. So It is possible.

Nested List

We can have list within list. We can access the list and its individual elements and modify those values.


List functions

1) list.append(x) : List can dynamically grow. It is not fixed size data structure. To add element at last we can use append() function.


2) list.extend(iterable) : To add more than one elements use extends(). As shown in above example append() can add one element at a time.


3) list.insert(i, x) : inserts the element  x at an index i using insert() function. Elements to the right of the inserted element will moved to right by 1 index.


4) list.remove(x) : removes the element x from list. If that value is not present in list the ValueError Exception is raised.


5) list.pop(i) : removes the element at the index i. If the index is not passed it removes the last element.


6) list.clear() : removes all the elment from list and makes it empty list.


7) list.index(x) : returns the index of element x in the list. If the element is not present, it gives value Error.


8) list.count(x) : returns occurrence of element x in the list.

9) list.reverse() : reverse the element of the list.


10) list.sort() : sorts the elements of the list.


11) list.copy() : returns a copy of the list.


12) delete statement (del) : deletes the elements from list provided indices.


13) slicing in a list : we slice a list by providing start and end index. We have additional optional step. 

Syntax :
            list[start:end]  (end index will be excluded)
            list[start:end:step]
 


Thank you folks. If you like this Python 3 post, please do checkout my other post on  Django with Python 3 series

Most viewed

Understanding ASCII / Unicode character encoding format

Computer stores everything in binary format. The process of converting a value into binary is called Encoding and the the process of converting value from binary is called Decoding. value --> binary  ::     Encoding  binary --> value   ::    Decoding for example: A number 12 is stored in its binary format as below.               12 ---> 00001100 but how are the characters are stored? If we want to store a character 'a' what formatting should be used ? It easiest way is to map the characters to number and store the binary format of that number. So, the character 'a' is represented as number 97 is stored as 1100001.  ASCII: In ASCII encoding format, 128 unique characters are identified and mapped with numbers (mostly English). One byte is used to represent all the characters. It uses 7 bits to represent numeric value 0 to 127 (total 128 characters).  The most significant bit is a...