Q. What is data structure?
Ans. A way for computers to store and retrieve data.
A data structure may be selected or designed to store data for the purpose of working on it with various algorithms.
- Data structures are popular interview questions during technical exams at software companies.
- It's important to pick the proper data structure for the particular problem you're working on.
- An ill suited data structure could lead to poor performing programs.
- Python has 4 builtin data structures, and many more that can be imported to use in your various programs.
Q. What is lists in data structure?
Ans. A data structure that stores data in a linear fashion.
The elements, or 'data' inside of a list is typically homogeneous similar to an array in other languages like with other languages.
1) Append method - add data into a list. You can add one element at a time.
>>> empty = [] >>> empty.append(1) >>> empty.append(2) >>> print(empty) [1, 2] >>> >>> empty.append(40) >>> print(empty) [1, 2, 40] >>> >>> >>> empty.append("something something") >>> print(empty) [1, 2, 40, 'something something'] >>> data = [] >>> data.append(10) >>> data.append('20') >>> data.append(4+7) >>> data.append([6,7,8]) >>> print (data) [10, '20', 11, [6, 7, 8]]
2) Accessing elements in lists with the index (meaning everything starts counting from 0 - ZERO)
>>> coke = [2,4,6,8,10,12,14] >>> print(coke[0]) 2 >>> print(coke[4]) 10 >>> print(len(coke)) 7 >>> print(len(coke)-1) 6
3) Negative index in python
>>> print(coke[-1]) 14 >>> print(coke[-3]) 10
4) Slicing the list or taking range of numbers
index = [1,2,3,4] >>> print(index[0:3]) [1, 2, 3] # [::] returns everything >>> print(index[::]) [1, 2, 3, 4] # [::2] returns till index no. 2 but skips index no. 2 element >>> print(index[::2]) [1, 3] >>> index.extend([5,6,7,8,9,10,11,12,13]) >>> print(index[-7:-3]) [7, 8, 9, 10] # Reverse the list using [::-1] >>> print(index[::-1]) [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # Another way of reverse is by using function reverse() >>> index.reverse() >>> print(index) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
5) len() gets the size of the list (meaning counting each element in the list)
>>> my_data = [1,2,3,4,5,6,7,8] >>> size = len(my_data) >>> print(size) 8
6) Adding another list in the current list
>>> my_data = [1,2,3,4,5,6,7,8] >>> my_data.extend([100,200,300,400]) >>> print(my_data) [1, 2, 3, 4, 5, 6, 7, 8, 100, 200, 300, 400]
7) Counting similar elements in the list
>>> Apple = [30,40,30,30,30,60,70,80,80,90,100,100,200] >>> print(Apple.count(30)) 4 >>> print(Apple.count(100)) 2 >>> fruit = ["apple","apple","mango","mango","mango","banana","banana","cherry"] >>> print(fruit.count("mango")) 3 >>>
8) pop() will start counting from the last index in the list till index zero until you get error
>>> count = ["Hello","World","I","Am","Doctor","Strange"] >>> print(count.pop()) Strange >>> print(count.pop()) Doctor >>> print(count.pop()) Am >>> print(count.pop()) I >>> print(count.pop()) World >>> print(count.pop()) Hello >>> print(count.pop()) Traceback (most recent call last): File "<pyshell#116>", line 1, in <module> print(count.pop()) IndexError: pop from empty list
9) Insert in the list - this one is a bit tricky. insert(<index number>,<WHAT YOU WANT TO INSERT>)
>>> sentence = ["She","is","very","girl"] >>> sentence.insert(3,"beautiful") >>> print(sentence) ['She', 'is', 'very', 'beautiful', 'girl']
10) Using for or while loop to iterate over a list
(iterate - meaning it will repeat itself number of times until the condition is met. When a first set of instructions is executed again that is called iteration.)
a) Using FOR LOOP
>>> students = [100,200,300,400,500,600,700,800,900] >>> for i in students: print(i) 100 200 300 400 500 600 700 800 900 >>> students = [100,200,300,400,500,600,700,800,900] >>> for i in students: print(i,end=' ') 100 200 300 400 500 600 700 800 900
b) Using WHILE LOOP
>>> students = [100,200,300,400,500,600,700,800,900] >>> i = 0 >>> while i < len(students): print(students[i], end=' ') i += 1 100 200 300 400 500 600 700 800 900
11) A list within a list is called two dimensional list or 2D list
>>> grid_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print(grid_1) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> grid_2 = [ [1,2,3], [4,5,6], [7,8,9] ] >>> print(grid_2) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> >>> print(grid_2[0][0]) 1 >>> print(grid_2[0][1]) 2 >>> print(grid_2[1][1]) 5 >>> print(grid_2[2][0] * grid_2[2][2]) 63
12) Using NESTED LOOP to iterate through all of the rows and columns of a 2D list
>>> for row in range(len(grid_2)): for col in range(len(grid_2)): print(grid_2[row][col], end=' ') 1 2 3 4 5 6 7 8 9 >>>
13) List comprehensions. A shorthand version for creating lists.
>>> ball = [] >>> for i in range(10): ball.append(i) print(ball) [0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> >>> a_ball = [i for i in range(10)] >>> print(a_ball) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> >>> b_ball = [i for i in range(100) if i % 2 == 0] >>> print(b_ball) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98] >>> >>> c_ball = [x for x in range(1,3) for y in range(1,7)] >>> print(c_ball) [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] >>>
14) Tuples - Tuples are very similar to lists with the exception that tuples are immutable. Meaning that the object is immutable.
>>> point = (1,5) >>> print(point) (1, 5) >>> print(point.count(1)) 1 >>> print(point.index(5)) 1 >>> print(len(point)) 2 >>> print(point[::]) (1, 5) >>> print(point[1]) 5
a) Iterating over tuples
>>> point = (1,5) >>> for x in point: print(x,end=' ') 1 5 >>>
15) Dictionaries - Also known as hash tables, hash maps, or associative arrays in other languages.
Contains key/value mappings. A very ubiquitous data structure. Used for database indexing.
Create them using curly braces.
>>> vowels_1 = {'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4} >>> print(vowels_1) {'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4} >>> vowels_2 = { 'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4 } >>> print(vowels_2) {'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4} >>> a) Accessing elements in dictionaries. Can use subscript notation to get the value. >>> print(vowels_1['a']) 0 >>> print(vowels_1.get('a')) 0 >>> print(vowels_1.items()) #returns all key/value mappings dict_items([('a', 0), ('e', 1), ('i', 2), ('o', 3), ('u', 4)]) >>> print(vowels_1.pop('o')) 3 >>> print(vowels_1.keys()) dict_keys(['a', 'e', 'i', 'u']) >>> print(vowels_1.values()) dict_values([0, 1, 2, 4]) >>>
16) Sets - Similar to sets in mathematics. It can't hold no duplicates.
Internally implements a dictionary. Also uses curly braces. Order is not preserved.
a) Common set functions
>>> letters = {'a', 'A', 'a', 'b', 'b', 'c', 'd', 'e', 'e', 'f'} >>> print(letters) {'c', 'f', 'b', 'd', 'e', 'A', 'a'} >>> >>> constants = {'a', 'b', 'c'} >>> print(letters.union(constants)) # returns first set combined with elements of second {'c', 'f', 'b', 'd', 'e', 'A', 'a'} >>> print(letters.difference(constants))# returns a set that subtracts the second from the first {'f', 'A', 'd', 'e'} >>> print(letters.intersection(constants)) # returns elements common {'c', 'a', 'b'} >>> print(letters.symmetric_difference(constants)) # (A-B) U (B-A) {'f', 'd', 'e', 'A'} >>> for x in letters: print(x,end=' ') c f b d e A a >>>
Hope you like the tutorial.
Python is simple and fun to play with and yet very powerful. 🔥