Full Transcript

# EGCO3140 Data Structures & Algorithms: Dictionaries and Arrays ## Outcomes: 3, 4, 6, 8 ## Outline - Introduction to Dictionaries - Operations on Dictionaries - Arrays - Lists Vs Arrays - Applications of Data Structures ## Dictionaries in Python - Dictionary is a data structure which consists...

# EGCO3140 Data Structures & Algorithms: Dictionaries and Arrays ## Outcomes: 3, 4, 6, 8 ## Outline - Introduction to Dictionaries - Operations on Dictionaries - Arrays - Lists Vs Arrays - Applications of Data Structures ## Dictionaries in Python - Dictionary is a data structure which consists of a {key:value} pair within flower braces. - Each key is separated from its value by a colon. Multiple keys and values are separated by commas. - Dictionaries are also called Maps or Associative Arrays. - Keys are always unique in a dictionary, but values are not. **Syntax:** ```python dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3} ``` ## Adding / Updating / Accessing Items in Dictionary ```python D={'Reg':179087,'Name':'Abdullah','Age':89,'Course':'Diploma'} print(D) print("D[Reg]=", D['Reg']) print("D[Name]=", D['Name']) print("D[Age]=", D['Age']) print("D[Course]=", D['Course']) D['Marks']=95 print("D[Marks]=",D['Marks']) D['Course']='Advanced Diploma' print(D) ``` **Output:** ``` {'Reg': 179087, 'Name': 'Abdullah', 'Age': 89, 'Course': 'Diploma'} D[Reg]= 179087 D[Name]= Abdullah D[Age]= 89 D[Course]= Diploma D[Marks]= 95 {'Reg': 179087, 'Name': 'Abdullah', 'Age': 89, 'Course': 'Advanced Diploma', 'Marks': 95} ``` ## Deleting Items in a Dictionary - One or more items can be deleted using the `del` keyword. - Entire dictionary can be deleted by using the `clear()` function. - Specific values can be printed by using the `pop(key)` method. - Random items can be removed from the dictionary using `popitem()` method. ## Program ```python D={'Reg':179087,'Name':'Abdullah','Age':18,'Course':'Diploma'} print(D) D['Marks']=95 print("D[Marks]=",D['Marks']) D['Course']='Advanced Diploma' print(D) print('Deleting name from Dictionary:') del(D['Name']) print(D) print("Popping Age value from Dictionary:", D.pop('Age')) print(D) print("Randomly Popping value from Dictionary:",D.popitem()) print(D) D.clear() print(D) ``` **Output:** ``` {'Reg': 179087, 'Name': 'Abdullah', 'Age': 18, 'Course': 'Diploma'} D[Marks]= 95 {'Reg': 179087, 'Name': 'Abdullah', 'Age': 18, 'Course': 'Advanced Diploma', 'Marks': 95} Deleting name from Dictionary: {'Reg': 179087, 'Age': 18, 'Course': 'Advanced Diploma', 'Marks': 95} Popping Age value from Dictionary: 18 {'Reg': 179087, 'Course': 'Advanced Diploma', 'Marks': 95} Randomly Popping value from Dictionary: ('Marks', 95) {'Reg': 179087, 'Course': 'Advanced Diploma'} {} ``` ## Built-In Operations on Dictionaries Let 'M' be a dictionary which is initially empty. | Operation | Syntax | Example | Output / Map | |---|---|---|---| | Length | `len(M)` | `print(len(M))` | 0 / {} | | Insertion/Addition | `M['K']=1` | `M['K']=1` | - / {'K':1} | | | `M['B']=2` | `M['B']=2` | - / {'K':1,'B':2} | | Access values | `M['B']` | `print(M['B'])` | 2 / {'K':1, 'B':2} | | Access non-existent key | | `print(M['X'])` | Key Error / {'K':1, 'B':2} | | Access Keys | `M.keys()` | `print(M.keys())` | 'K','B' / {'K':1, 'B':2} | | Access values | `M.values()` | `print(M.values())` | 1,2 / {'K':1,'B':2} | | Access items | `M.items()` | `print(M.items())` | ('K':1),('B':2) / {'K':1,'B':2} | ## Exercises - Implement the following example as a dictionary in Python. (Countries - Keys) - Perform all the operations that can be performed on this dictionary. An image is present in the document with a diagram showing countries and their currency. ## Arrays in Python - An array is a homogenous data structure, that is, it can store values only of the same datatype. - We cannot create an array of strings or characters in Python. Only integers and float values can be stored in an array. - Arrays are not in-built in Python. - We have to import the array module to create arrays in Python. - So lists are more popular in Python than arrays. ## Array Creation ```python import array as arr A = arr.array('i', [1, 2, 3, 4, 5]) print ("The new created array is : ", end =" ") for i in range (0, 5): print (A[i], end =" ") print() B = arr.array('f', [2.7, 4.2, 1.3]) print ("The new created array is : ", end =" ") for i in range (0, 3): print (B[i], end =" ") ``` **Output:** ``` The new created array is : 12345 The new created array is : 2.700000047683716 4.199999809265137 1.29999995231628 ``` ## Difference Between Lists and Arrays | | LISTS | ARRAYS | |---|---|---| | Data Structure | Heterogenous data structure | Homogenous data structure | | Built-in | In-built in Python | Not in-built in Python | | Import required | Does not require any import statements | Requires the import of array module. | | Data Types | Strings and characters can be stored in lists | Strings/characters cannot be stored in Arrays. | | Example | L=[1,3.5,'C'] | Import array as arr A = arr.array('i', [1, 2, 3, 4, 5]) | ## Application of Data Structures - Lists and tuples are used for storing values of different data types. - Tuples are used in places where the values should not changed. - Lists are used for applications where values may need to change and be flexible. - The performance of tuples is better than that of lists. - Tuples can also be used as keys for dictionaries but lists cannot be used as keys. - Dictionaries are used in scenarios where we need to match one item(key) with another(value) and to show relationships between the stored values. ## Summary - Dictionaries - Arrays - Applications.

Use Quizgecko on...
Browser
Browser