Sunday, September 16, 2018

Python Ninja Bootcamp 7-List

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the List.

"List is an ordered collection of mutable elements".


Let's look at List example-

Create List


List is defined by placing the all the elements under brackets [ ] and separated by a comma.


list1 = [1,2,3,4,5]
print(list1)
>> [1,2,3,4,5]


We have an option in python that list can hold different object type (integer, float, string etc.).
list2=[1,1.5,'string']
print(list1)
>>[1,1.5,'string']

# len() Calculates the length of list
print(len(list2))
>>3


List Indexing


We can use the index operator [] to access an item in a list. Index starts from 0. 


my_list=[1,2,'three','four'] 
# Since, we have 4 elements in the list. So, the index starts from 0 to 3
print( my_list[0] ) # garbs the element at index 0
>>1

print( my_list[2] ) # garbs the element at index 2
>> 'three'

print( my_list[-1] ) # garbs the last element
>> 'four'


List Slicing


We can access a range of items in a list by using the colon.
We have my_list = [ 1 , 2 , 'three' , 'four' ] 

print( my_list[1:] ) # garbs everything from the first index 
>> [ 2 , 'three' , 'four' ] 


print( my_list[2:] ) # garbs everything from the second index 
>> [ 'three' , 'four' ]


print( my_list[:3] ) # garbs everything from first index 0 to third index(excluded)
>> [ 1 , 2 , 'three' ]

print( my_list[:-1] ) # garbs everything from 0 index to the last index(excluded)
>> [ 1 , 2 , 'three' ]

print( my_list[1:3:2] ) # garbs every 2nd element from the 1st index to 3th index(excluded) 
>> [2]

*Important Syntax
print( my_list[::-1] ) #  Reverse the list
>> [ 'four' , 'three' , 2 , 1 ]


List Properties


Mutability

List is mutable, the elements within it can be changed or replaced, unlike string or tuple.

We can use the assignment operator (=) to change an item or a range of items.
We have my_list = [ 1 , 2 , 'three' , 'four' ].


my_list[2] = 3  # 3rd element gets changed from 'three' to 3 
print(my_list)
>> [ 1 , 2 , 3 , 'four' ] 

We can also use the + operator to combine/concatenate two lists.
We have my_list = [ 1 , 2 , 'three' , 'four' ]

my_list = my_list + ['New Item']
print( my_list )
>> [ 1 , 2 , 'three' , 'four' , 'New Item' ]


We can also use the * operator to repeats a list for the given number of times.
We have my_list = [ 1 , 2 , 'three' , 'four' ]

print( my_list * 2 )
>> [ 1 , 2 , 'three' , 'four' , 1 , 2 , 'three' , 'four' ]


Built-in Function


Methods that are available with list object in Python.

We have my_list = [ 1 , 2 , 'three' , 'four' ]

my_list.append('new item') # add the element to the end of the list
print( my_list )
>> [ 1 , 2 , 'three' , 'four' , 'new item' ]

Note: If press TAB after my_list. in the Juypter notebook. It will show the list of functions available in the string.

We have my_list = [ 1 , 2 , 'three' , 'four' ]

my_list.pop(0) # Remove and return item at given index(by default last element)
print( my_list ) # element at index zero is removed
>> [ 2 , 'three' , 'four' ]

We have my_list = [ 'a' , 'e' , 'x' , 'b' , 'z' ]

my_list = [ 'a' , 'e' , 'x' , 'b' , 'z' ]
my_list.reverse() # reverse the order of the item in the lists
print( my_list )
>>[ 'z' , 'b' , 'x' , 'e' , 'a' ]

We have my_list = [ 'a' , 'e' , 'x' , 'b' , 'z' ]

my_list = [ 'a' , 'e' , 'x' , 'b' , 'z' ]
my_list.sort( ) # Sort the items in the list in ascending order
print( my_list )
>> [ 'a' , 'b' , 'e' , 'x' , ' z' ]

Practice other built-in functions available in the list.


Nested List


List could have another list as an item. This is called nested list.

# Define three lists

list_1 = [ 1, 2, 3 ]
list_2 = [ 4, 5, 6 ] 
list_3 = [ 7, 8, 9 ]



matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
print( matrix )
>>[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

We can also use indexing in the nested list.
we have matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 

print( matrix[0] ) # garbs the first list from the matrix list
>> [ 1, 2, 3 ]

print( matrix[0][2] ) # garbs the first item of the first list in the matrix list
>> 3

print( matrix[1][0] ) # garbs the first item of the second list in the matrix list
>> 4

print( matrix[2][2] ) # garbs the third item of the third list in the matrix list
>> 9


List Comprehension

List comprehension is an elegant way to create a new list from an existing list in Python.
List comprehension consists of an expression followed by for statement inside square brackets.
# Build a list comprehension to grab even numbers between 1 t0 9
l = [1,2,3,4,5,6,7,8,9]
print( [i for i in l if i%2==0] )
>> [ 2, 4, 6, 8 ]

Don't worry if you don't understand this section. Feel free to skip this topic since we will discuss list comprehension in detail.




EDITS ARE WELCOMED!!

In the next Blog, we will discuss Dictionary  

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-8-dictionary.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment