Data Structure in Python

In [1]:
# Tuple (cannot change)
a = (1, 2, 3)
print(a)
(1, 2, 3)
In [3]:
# Lists

mylist = [1, 2, 3]
print(); print("Zeroth Value: %d" % mylist[0])

mylist.append(4)
print(); print("List Length: %d" % len(mylist))

print()
for value in mylist:
    print(value)
Zeroth Value: 1

List Length: 4

1
2
3
4
In [4]:
# Dictionaries

mydict = {'a': 1, 'b': 2, 'c': 3}
print(); print("A value: %d" % mydict['a'])

mydict['a'] = 11
print(); print("A value: %d" % mydict['a'])

print(); print("Keys: %s" % mydict.keys())

print(); print("Values: %s" % mydict.values())

print()
for key in mydict.keys(): 
    print(mydict[key])
A value: 1

A value: 11

Keys: dict_keys(['a', 'b', 'c'])

Values: dict_values([11, 2, 3])

11
2
3

Exercise Question 1:

Given a two list. Create a third list by picking an odd-index element from the first list and even index elements from second.

In [6]:
listOne = [3, 6, 9, 12, 15, 18, 21]
listTwo = [4, 8, 12, 16, 20, 24, 28]

listThree = list()
oddElements = listOne[1::2]
print(); print("Element at odd-index positions from list one")
print(oddElements)

EvenElement = listTwo[0::2]
print(); print("Element at odd-index positions from list two")
print(EvenElement)

print(); print("Printing Final third list")
listThree.extend(oddElements)
listThree.extend(EvenElement)
print(listThree)
Element at odd-index positions from list one
[6, 12, 18]

Element at odd-index positions from list two
[4, 12, 20, 28]

Printing Final third list
[6, 12, 18, 4, 12, 20, 28]

Exercise Question 2:

Given an input list removes the element at index 4 and add it to the 2nd position and also, at the end of the list

In [7]:
sampleList = [34, 54, 67, 89, 11, 43, 94]
print(); print("Origigal list ", sampleList)

element = sampleList.pop(4)
print(); print("List After removing element at index 4 ", sampleList)

sampleList.insert(2, element)
print(); print("List after Adding element at index 2 ", sampleList)

sampleList.append(element)
print(); print("List after Adding element at last ", sampleList)
Origigal list  [34, 54, 67, 89, 11, 43, 94]

List After removing element at index 4  [34, 54, 67, 89, 43, 94]

List after Adding element at index 2  [34, 54, 11, 67, 89, 43, 94]

List after Adding element at last  [34, 54, 11, 67, 89, 43, 94, 11]

Exercise Question 3:

Given a list slice it into a 3 equal chunks and rever each list

In [8]:
sampleList = [11, 45, 8, 23, 14, 12, 78, 45, 89]
print(); print("Origigal list ", sampleList)

length = len(sampleList)
chunkSize  = int(length/3)
start = 0
end = chunkSize
for i in range(1, 4, 1):
  indexes = slice(start, end, 1)
  listChunk = sampleList[indexes]
  print("Chunk ", i , listChunk)
  print("After reversing it ", list(reversed(listChunk)))
  start = end
  if(i != 2):
    end +=chunkSize
  else:
    end += length - chunkSize
Origigal list  [11, 45, 8, 23, 14, 12, 78, 45, 89]
Chunk  1 [11, 45, 8]
After reversing it  [8, 45, 11]
Chunk  2 [23, 14, 12]
After reversing it  [12, 14, 23]
Chunk  3 [78, 45, 89]
After reversing it  [89, 45, 78]

Exercise Question 4:

Given a list iterate it and count the occurrence of each element and create a dictionary to show the count of each element

In [9]:
sampleList = [11, 45, 8, 11, 23, 45, 23, 45, 89]
print("Origigal list ", sampleList)

countDict = dict()
for item in sampleList:
  if(item in countDict):
    countDict[item] += 1
  else:
    countDict[item] = 1
  
print("Printing count of each item  ",countDict)
Origigal list  [11, 45, 8, 11, 23, 45, 23, 45, 89]
Printing count of each item   {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}

Exercise Question 5:

Given a two list of equal size create a set such that it shows the element from both lists in the pair

In [10]:
firstList = [2, 3, 4, 5, 6, 7, 8]
print("First List ", firstList)

secondList = [4, 9, 16, 25, 36, 49, 64]
print("Second List ", secondList)

result = zip(firstList, secondList)
resultSet = set(result)
print(resultSet)
First List  [2, 3, 4, 5, 6, 7, 8]
Second List  [4, 9, 16, 25, 36, 49, 64]
{(6, 36), (8, 64), (4, 16), (5, 25), (3, 9), (7, 49), (2, 4)}

Exercise Question 6:

Given a following two sets find the intersection and remove those elements from the first set

In [11]:
firstSet  = {23, 42, 65, 57, 78, 83, 29}
secondSet = {57, 83, 29, 67, 73, 43, 48}

print("First Set ", firstSet)
print("Second Set ", secondSet)

intersection = firstSet.intersection(secondSet)
print("Intersection is ", intersection)

for item in intersection:
  firstSet.remove(item)
print("First Set after removing common element ", firstSet)
First Set  {65, 42, 78, 83, 23, 57, 29}
Second Set  {67, 73, 43, 48, 83, 57, 29}
Intersection is  {57, 83, 29}
First Set after removing common element  {65, 42, 78, 23}

Exercise Question 7:

Given two sets, Checks if One Set is Subset or superset of Another Set. if the subset is found delete all elements from that set

In [12]:
firstSet  = {57, 83, 29}
secondSet = {57, 83, 29, 67, 73, 43, 48}
print("First Set ", firstSet)
print("Second Set ", secondSet)
print("First set is subset of second set -", firstSet.issubset(secondSet))
print("Second set is subset of First set - ", secondSet.issubset(firstSet))
print("First set is Super set of second set - ", firstSet.issuperset(secondSet))
print("Second set is Super set of First set - ", secondSet.issuperset(firstSet))

if(firstSet.issubset(secondSet)):
  firstSet.clear()
  
if(secondSet.issubset(firstSet)):
  secondSet.clear()

print("First Set ", firstSet)
print("Second Set ", secondSet)
First Set  {57, 83, 29}
Second Set  {67, 73, 43, 48, 83, 57, 29}
First set is subset of second set - True
Second set is subset of First set -  False
First set is Super set of second set -  False
Second set is Super set of First set -  True
First Set  set()
Second Set  {67, 73, 43, 48, 83, 57, 29}

Exercise Question 8:

Iterate a given list and Check if a given element already exists in a dictionary as a key’s value if not delete it from the list

In [13]:
rollNumber  = [47, 64, 69, 37, 76, 83, 95, 97]
sampleDict  ={'Jhon':47, 'Emma':69, 'Kelly':76, 'Jason':97} 

print("List -", rollNumber)

print("Dictionary - ", sampleDict)

rollNumber[:] = [item for item in rollNumber if item in sampleDict.values()]
print("after removing unwanted elemnts from list ", rollNumber)
List - [47, 64, 69, 37, 76, 83, 95, 97]
Dictionary -  {'Jhon': 47, 'Emma': 69, 'Kelly': 76, 'Jason': 97}
after removing unwanted elemnts from list  [47, 69, 76, 97]

Exercise Question 9:

Given a dictionary get all values from the dictionary and add it in a list but don’t add duplicates

In [14]:
sampleList = [87, 52, 44, 53, 54, 87, 52, 53]
print("Original list", sampleList)

sampleList = list(set(sampleList))
print("unique list", sampleList)

tuple = tuple(sampleList)
print("tuple ", tuple)

print("Minimum number is: ", min(tuple))
print("Maximum number is: ", max(tuple))
Original list [87, 52, 44, 53, 54, 87, 52, 53]
unique list [44, 52, 53, 54, 87]
tuple  (44, 52, 53, 54, 87)
Minimum number is:  44
Maximum number is:  87

Functions in Python

In [16]:
# Sum function
def mysum(x, y):
    return x + y

# Test sum function
mysum(1, 3)
Out[16]:
4
In [17]:
# Subtration function
def mysum(x, y):
    return x - y

# Test subtraction function
mysum(8, 3)
Out[17]:
5
In [18]:
# Multiplication function
def mysum(x, y):
    return x * y

# Test multiplication function
mysum(4, 3)
Out[18]:
12
In [19]:
# Divition function
def mysum(x, y):
    return x / y

# Test division function
mysum(27, 3)
Out[19]:
9.0
In [20]:
# Power function
def mysum(x, y):
    return x ** y

# Test power function
mysum(3, 3)
Out[20]:
27