Data Warehousing, BI and Data Science

27 November 2020

Python: List, Tuple, Dictionary, Set

Filed under: Python — Vincent Rainardi @ 4:54 am

List: Use append function to add an element.
Use pop and remove functions to remove an element.

a = ['A','B',1,2]
print(a)
Output: ['A', 'B', 1, 2]

a.pop()
print(a)
Output: ['A', 'B', 1]

a.append('C')
print(a)
Output: ['A', 'B', 1, 'C']

a.remove('B')
print(a)
Output: ['A', 1, 'C']

List operators: * and +
Functions: len, max, sorted, index

print(a*2)
Output: ['A', 1, 'C', 'A', 1, 'C']

print(a+a)
Output: ['A', 1, 'C', 'A', 1, 'C']

a = [2,3,5,1]
print(len(a), max(a), sorted(a), a[0:3], a.index(5))
Output: 4 5 [1, 2, 3, 5] [2, 3, 5] 2

b = [[1,2],[3,4],[5,6]]
print(b[1], '|', b[0][1])
Output: [3, 4] | 2

a[n][m] means list n, element m
a[n:m] means list n to list m-1

a = [[1,2,3],[4,5,6],[7,8,9]]
print(a[0][0])
Output: 1

print(a[:])
Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(a[0:1]) 
Output: [[1, 2, 3]]

print(a[0:2]) 
Output: [[1, 2, 3], [4, 5, 6]]

print(a[1:3]) 
Output: [[4, 5, 6], [7, 8, 9]]

a = [1,2]
b = [3,4]
a.extend(b)
print(a)
Output: [1, 2, 3, 4]

a.insert(3,1)
print(a, a.count(1))
Output: [1, 2, 3, 1, 4] 2

Tuple is like a list but once created it can’t be changed.

a = ("A", 1, "B", 2)
print(a, a[1])
Output: ('A', 1, 'B', 2) 1

b = list(a)
print(b)
Output: ['A', 1, 'B', 2]

c = tuple(b)
print(c)
Output: ('A', 1, 'B', 2)

d = ([1,2],"A")
print(d)
Output: ([1, 2], 'A')

Dictionary uses labels instead of index.
To get the value of a key which may not exist, use get.
To add a key value pair, use =.
We can convert a dictionary to a list. Use values() to get the values, keys() to get the keys.
We can use dict to create a dictionary.
Use update to add a key value pair and del to remove a key value pair.

a = {'Name': 'Harry', 'Age': 11}
print(a, a['Name'])
Output: {'Name': 'Harry', 'Age': 11} Harry

Name = a.get('Name')
City = a.get('City','N/A')
print(Name, City)
Output: Harry N/A

a['City'] = 'London'
print(a)
Output: {'Name': 'Harry', 'Age': 11, 'City': 'London'}

print(list(a.values()), list(a.keys()), 'Harry' in list(a.values()))
Output: ['Harry', 11, 'London'] ['Name', 'Age', 'City'] True

a = dict(Name = 'Harry', Age = 11)
a.update({'City':'London'})
print(a, len(a))
Output: {'Name': 'Harry', 'Age': 11, 'City': 'London'} 3

del (a['City'])
print(a, len(a))
Output: {'Name': 'Harry', 'Age': 11} 2

a = {1: ['Harry',11], 2:['Ron',10]}
print(a)
Output: {1: ['Harry', 11], 2: ['Ron', 10]}

Using set we can do Venn diagram operations such as union, intersection and difference

a = [1,2,2,3,1]
b = set(a)
c = {3,4}
print(a,b,c)
Output: [1, 2, 2, 3, 1] {1, 2, 3} {3, 4}

print(b.union(c), b.intersection(c), b.difference(c), b.symmetric_difference(c))
Output: {1, 2, 3, 4} {3} {1, 2} {1, 2, 4}

a = [1,2,3,4]
print(a[-1])
Output: 4

S = "I love Python"
print(S[2:6], S[-11:-7])
Output: love love

L = [1,2,3]
print(L*2)
Output: [1, 2, 3, 1, 2, 3]

L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print(L[2:])
Output: [[7, 8, 9, 10]]

C = [2, 5, 9, 12, 13, 15, 16, 17, 18, 19]
F = [2, 4, 5, 6, 7, 9, 13, 16]
H = [1, 2, 5, 9, 10, 11, 12, 13, 15]
print(sorted(set(C) & set(F) & set(H)))
Output: [2, 5, 9, 13]

print(sorted(set(C) & set(F) - set(H)))
Output: [16]

d = C + F + H
print(d)
Output: [2, 5, 9, 12, 13, 15, 16, 17, 18, 19, 2, 4, 5, 6, 7, 9, 13, 16, 1, 2, 5, 9, 10, 11, 12, 13, 15]

L = []
for i in d:
    if d.count(i) == 2: L.append(i)
print(sorted(list(set(L))))
Output: [12, 15, 16]

L = []
for i in range(1,21):
    if d.count(i) == 0: L.append(i)
print(sorted(list(set(L))))
Output: [3, 8, 14, 20]

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

<span>%d</span> bloggers like this: