﻿ Python Sample Papers: Data Structure

## Python Data Structure Exercises for Beginners

``````
Solution
listOne = [3, 6, 9, 12, 15, 18, 21]
listTwo = [4, 8, 12, 16, 20, 24, 28]
listThree = list()

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

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

print("Final third list")
listThree.extend(oddElements)
listThree.extend(EvenElement)
print(listThree)
```
```

### Output

>>>
Element at odd-index positions from list one
[6, 12, 18]
Element at odd-index positions from list two
[4, 12, 20, 28]
Final third list
[6, 12, 18, 4, 12, 20, 28]

``````
testList = [8, 17, 67, 89, 11, 43, 55]

print("Org. list ", testList)
val = testList.pop(3)
print("Updated List after removing no. at pos. 3 ", testList)

testList.insert(2, val)
print("Now add it at ind. 2 ", testList)

testList.append(val)
print("After Add at last ", tsetList)
```
```
>>>
Org. list [8, 17, 67, 89, 11, 43, 55]
Updated List after removing no. at pos. 3 [8, 17, 67, 11, 43, 55]
Now add it at ind. 2 [8, 17, 89, 67, 11, 43, 55]
After Addition at last [8, 17, 89, 67, 11, 43, 55, 89]

``````

Lst1 = [56, 27, 8, 91, 44, 12, 19, 27, 63]
print("Org. ", Lst1)

ln = len(Lst1)
spSize = int(ln/3)
st = 0
end = spSize
for i in range(1, 4, 1):
ind = slice(st, end, 1)
tmpL= Lst1[ind]
print("Part ", i , tmpL)
print("Reversed ", list(reversed(tmpL)))
st = end
if(i != 2):
end +=spSize
else:
end += ln - spSize

```
```
>>>
Initial: [56, 27, 8, 91, 44, 12, 19, 27, 63]
Part 1 [56, 27, 8]
Reversed [8, 27, 56]
Part 2 [91, 44, 12]
Reversed [12, 44, 91]
Part 3 [19, 27, 63]
Reversed [63, 27, 19]

``````
tList = [14, 20, 88, 14, 20, 38, 20]
print("list ", tList)

cntDict = dict()
for i in tList:
if(i in cntDict):
cntDict[i] += 1
else:
cntDict[i] = 1

print("Count Detail ",cntDict)
```
```
>>>
list [14, 20, 88, 14, 20, 38, 20]
Count Detail {14: 2, 20: 3, 88: 1, 38: 1}

``````
L1 = [2, 4, 6, 8]
print("List 1 : ", L1)

L2 = [4, 8, 12, 16]
print("List 2 : ", L2)

L3 = zip(L1, L2)
res_Set = set(L3)
print("o/p is :",res_Set)
```
```
>>>
List 1 : [2, 4, 6, 8]
List 2 : [4, 8, 12, 16]
o/p is :{(2, 4), (4, 8), (6, 12), (8, 16)}

``````
F_Set = {32, 42, 51, 57, 40, 79, 33}
S_Set = {57, 79, 33, 67, 73, 43, 48}

print("First  ", F_Set)
print("Second  ", S_Set)

x = F_Set.intersection(S_Set)
print("Intersection set ", x)
for i in x:
F_Set.remove(i)

print("First Set after deleting common numbers ", F_Set)

```
```
>>>
First {51, 42, 40, 79, 32, 57, 33}
Second {67, 73, 43, 48, 79, 57, 33}

Intersection set {57, 79, 33}
First Set after deleting common numbers {51, 42, 40, 32}

``````
Set_A = {57, 83, 29}
Set_B = {57, 83, 29, 67, 73, 43, 48}

print("A ", Set_A)
print("B ", Set_B)

print("A is subset of B -", Set_A.issubset(Set_B))
print("B is subset of A - ", Set_B.issubset(Set_A))

print("A is Super set of B - ", Set_A.issuperset(Set_B))
print("B is Super set of A - ", Set_B.issuperset(Set_A))

if(Set_A.issubset(Set_B)):
Set_A.clear()

if(Set_B.issubset(Set_A)):
Set_B.clear()

print("A : ", Set_A)
print("B : ", Set_B)
```
```
>>>
A {57, 83, 29}
B {67, 73, 43, 48, 83, 57, 29}

A is subset of B - True
B is subset of A - False

A is Super set of B - False
B is Super set of A - True

A : set()
B : {67, 73, 43, 48, 83, 57, 29}

``````
EmpID = [55, 83, 69, 94, 22, 83, 30, 97]
empDict ={'Karan':55, 'shruti':69, 'Iti':22, 'Sunil':97}

print("List -", EmpID)
print("Dictionary - ", empDict)

EmpID[:] = [i for i in EmpID if i in empDict.values()]
print("Updated list ", EmpID)
```
```
>>>
List - [55, 83, 69, 94, 22, 83, 30, 97]
Dictionary - {'Karan': 55, 'shruti': 69, 'Iti': 22, 'Sunil': 97}
[55, 69, 22, 97]
Updated list [55, 69, 22, 97]

``````
sale ={'jan':80, 'feb':31, 'mar':81, 'Apr':42, 'May':31, 'Jun':47,
'Jul':73, 'Aug':42, 'Sep':73}

print("Dictionary's values - ", sale.values())

saleList = list()
for val in sale.values():
if val not in saleList:
saleList.append(val)
print("Final :", saleList)
```
```
>>>
Dictionary's values - dict_values([80, 31, 81, 42, 31, 47, 73, 42, 73])
final : [80, 81, 47]

``````
Lst1 = [32, 40, 88, 53, 54, 32, 40, 53]

print("Org: ", Lst1)

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

tuple = tuple(Lst1)
print("tuple ", tuple)

print("Minimum  : ", min(tuple))
print("Maximum  : ", max(tuple))

```
```
>>>
Org: [32, 40, 88, 53, 54, 32, 40, 53]
unique list [88, 40, 53, 54, 32]
tuple (88, 40, 53, 54, 32)
Minimum : 32
Maximum : 88