list

#list is a class
#list is a type
#list is an iterable
#list is a sequence
#list is mutable
#list elements are indexed
#list is growable
#list can contain heterogeneous elements

Since list is iterable and programming concept of iterator is implemented in list, concept of for loop is applicable on the list. 

 #creating list objects



 







As list elements are iterable, we can apply for loop and while loop as below





















List index works both direction -1 index will fetch last element and -2 will access second last and so on but if there are no index available, we get error.













#how to delete element

del listname[index]

#how to update element

listname[index] = element

Remember if index is out of defined range, we get index of range error.

List is growable -> since it is a class, it has predefined methods to update the size. Can be achieved with append to add at last. To access member function we can use (.) operator. 

Syntax - Object.memberFunction()

Methods in list 

insert - To insert at certain place with certain value
extend - To extend existing list with other iterable
sort- To sort list
remove - To remove element
reverse - To reverse list

















Packing and upacking in list -> To add elements into the list and distribute element from list to individual elements. Very useful concept, number of variable must be equal while unpacking else we will get error.














Built in function - This method can be applied on any iterable

max(), min(), sorted(),len(),sum()

>>> help('sorted')
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
>>> a,b,c = 2,1,3
>>> l = [a,b,c]
>>> l
[2, 1, 3]
>>> sorted(l)
[1, 2, 3]
>>> l
[2, 1, 3]
>>> l1 = sorted(l)
>>> l1
[1, 2, 3]
>>>

Conversion function
We can convert any other iterable into list using list() conversion method.

l = list() - creates empty list
l = list(1) - Error integer is not iterable
l = list( range(5)) - return [0,1,2,3,4]

** expects only one iterable not multiple.

>>> l = list()
>>> l
[] 
>>> l = list(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list expected at most 1 argument, got 3
>>> l = list(range(5))
>>> l
[0, 1, 2, 3, 4]
>>>
**relational operators works in list

such as > , <. and operators such as concatenation and multiplier

>>> x1 = [1,2,3]
>>> x2 = [2,3,4,5]
>>> x2 > x1
True
>>> x1 > x2
False
>>> l1 = x1 + x2
>>> l1
[1, 2, 3, 2, 3, 4, 5]
>>>
Slicing Operator: 

[start : stop :  step ]

Some useful information from help section
slicing operator
a[start:end:step]

More on list function : can be check using help menu




List Comprehension
[expression for expression in iterable]
Example- 
>>> [x for x in range(0,6)]
[0, 1, 2, 3, 4, 5]
Used for precise coding. 






Comments