Wednesday, April 15, 2020

Data Structure in python - LIST

Data Structure in python:
Def: Data Structure is a logical arrangement of data that imposes some protocol(rules) on how the data will be arranged, modified and accessed. Data structure are usually temporary storage only used for efficient data manipulation.
Types of data structure in python:
1. Inbuilt Data Structure: List, Tuple, Set, Dictionary, String.
2. Used Defined Data Structure : Stack, Queue, Tree etc


     List     
Note: Its a collection of object which can be of any type and of any length, enclosed
with square brackets.
Properties:
1. enclosed within square brackets [ ]
2. Mutable
3. can store any kind of object and even list inside list
4. list has no size limit
5. Elements don’t have to be ordered or of same type

Creating List
Example
Remarks
ls=[1,2,4,6,8]
1 D list of integers.Its a homogeneous list i.e all the elements are of same type
ls=[1,3.14,’hello’]
1 D list of Heterogeneous(elements of different types) elements
ls=[[1,2,3],[4,5,6]]
2 D list of list
ls=[]
Creating an empty list

Accessing List:
Note: Every list has index , which can be used to access the elements of the list. List data structure maintains both positive index and negative index
Index:
Ls=[4.’hello’,45,3.14]
Positive index
0
1
2
3


4
Hello
45
3.14
-4
-3
-2
-1
Negative index

  
Code Example
Remarks
ls=[4,’hello’,45,3.14]
print(ls[1])
Output: hello
Using positive index of the list to access ‘hello’ from ls which al index value of 1
Note: +ve index starts from 0 and from left to right
ls=[4,’hello’,45,3.14]
print(ls[-1])
Output: 3.14
Using negative index of the list to access 3.14 which is at index value of -1
Note: -ve index starts from -1 and from right to left
ls=[4,’hello’,45,3.14]
for i in ls:
print(i)
Output:
4
Hello
45
3.14
Using a for loop to access the element of list
Ls1=[[1,2,3,4],[5,6,7,8]]
Val=Ls1[1][1]
print(Val)
Output: 6
Assess the items of a 2 dimensional list. The first index indicate the row and the second index indicate the column number

Adding/Appending Element to this list
Code Example
Remarks
Ls=[]
Ls.append(‘hello’)
Adds a new element ‘hello ’ to the end of the list
Ls1=[1,2]
Ls2=[3,’Hello’]
Ls1.extend(Ls2)
Add the content of List Ls2 to List Ls1 and make the List Ls1 bigger
Ls1=[4,5,6]
Ls1.insert(‘hello’,2)
Output: [4,5.’hello’,6]
Insert function helps to insert item/Object to a specific index location(index 2 in this example) of the list
Ls=[1,2,3]
Ls[2]=’hello’
Output:[1,2,’hello’]
Overwrites the index with the supplied value. Note we cant refer to a index which does not exists in the list

Removing element from a list
Code Example
Remarks
Ls=[1,2,3,4,5,6,7]
del Ls[2]
print(Ls)
Output:
[1,2,4,5,6,7]
Remove a element at index 2 of the list Ls
Ls=[1,2,3,4,5,6,7]
print(Ls.pop(2))
print(Ls)
Output:
3
[1,2,4,5,6,7]
Removes the element at index 2 from the list Ls and also returns the element removed


Operation on List
Operation on List
Example
Remarks
Joining list
Ls1=[1,2,3,4,5]
Ls2=[6,7,8,9,10]
print(Ls1+Ls2)
Output:
[1,2,3,4,5,6,7,8,9,10]
‘+’ operator can be used to join two or more list
Replicating list
Ls1=[1,2,3,4]
print(Ls1*2)
Output:
[1,2,3,4,1,2,3,4]
‘*’ operator can be used to repeat the elements of the list
Slicing the list
Ls1=[1,2,3,4,5,6,7,8,9]
newLs=Ls1[1:5]
print(newLs)
Output:
[2,3,4,5]
Can get a slice of element form the list.
Syntax:
List_name[start_index :  end_index]
Note: end_index in not inclusive
Striding the List
Ls1=[1,2,3,4,5,6,7,8,9]
newLs=Ls1[1:8:2]
print(newLs)
Output:
[2,4,6,8]
Can get elements from the list but not including all the element but skipping some
Syntax:
List_name[start_index : end_index : step_value] 


Most common Function used on list
Function syntax
Example
Remarks
List_name.index(item)
Ls=[23,45,67,89]
print(Ls.index(67))
Output:
2
Returns the index of the first matched item in the list
List_name.clear()
Ls=[23,45,67,89]
Ls.clear()
print(Ls)
Output:
[]
Removes all the item from the list and makes an empty list
List_name.count(item)
Ls=[34,34,56,34,72,34]
print(Ls.count(34))
Output:
4
Return the number of occurrence of the item in the list
List_name.reverse()
Ls=[34,’a’,45,22.5]
Ls.reverse()
print(Ls)
Output:
[22.5,45,’a’,34]
It reverses the list and its done in place hence no new list is created
List_name.sort()
Ls=[5,8,3,1,78,45]
Ls.sort()
print(Ls)
Output:
[1,3,5,8,45,78]
Sorts the list in place in ascending order
Note: we can use reverse=True argument to sort in descending order


List Comprehension:
Its a way to construct new list from the existing list. Its just using loop to construct new list but writing the loop in a fancy way
Note:
1. Easy and compact way to form new list
2. Conditional statement can be used in List Comprehension
3. Nested for loop can be used to in List Comprehension
4. Complicated loop should not written in List Comprehension as it is not user friendly.
5. Every List comprehension can be written using for loop but the reverse is not True

Creating a list using a for loop
Creating a list using list comprehension
Q. A python program to create a list 10 of integers staring from 0.
Ls=[]
for i in range(10):
Ls.append(i)
print(Ls)

Output:
[0,1,2,3,4,5,6,7,8,9]

Ls=[ i for i in range(10)]
print(Ls)

Output:
[0,1,2,3,4,5,6,7,8,9]
Q. A python program to construct a list which contain the square of all the elements of the given list Ls=[2,3,4,5,6]

Ls=[2,3,4,5,6]
newLs=[]
for i in Ls:
newLs.append( i**2 )
print(Ls)

Output:
[4,9,16,25,36]

Ls=[2,3,4,5,6]
newLs=[ i**2 for i in Ls ]
print(Ls)

Output:
[4,9,16,25,36]
Q. A python program to construct a list of first 10 even number
ls=[]
for i in range(20):
    if i%2==0:
        ls.append(i)

print(ls)
Output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

ls=[]
ls=[ i  for i in range(20) if i%2==0]
print(ls)

Output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]


References:
3.      Informatics Practices by Sumita Arora
4.     Python Docs
Share: