Category Archives: Python

14.4 Remove Dictionary Items

Removing Items There are several methods to remove items from a dictionary: Example The pop() method removes the item with the specified key name:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.pop(“model”)print(thisdict) Output:{‘brand’: ‘Ford’, ‘year’: 1964} Example The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict.popitem()print(thisdict) Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’} Example The del keyword removes the item with… Read More »

14.3 Add Dictionary Items

Adding Items Adding an item to the dictionary is done by using a new index key and assigning a value to it: Example thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict[“color”] = “red”print(thisdict) Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964, ‘color’: ‘red’} Update Dictionary The update() method will update the dictionary with the items from a given argument. If the item does not exist, the… Read More »

14.2 Change Dictionary Items

Change Values You can change the value of a specific item by referring to its key name: Example Change the “year” to 2018:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}thisdict[“year”] = 2018 Output:{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 2018} Update Dictionary The update() method will update the dictionary with the items from the given argument. The argument must be a dictionary, or an iterable object… Read More »

14.1 Access Dictionary Items

Accessing Items You can access the items of a dictionary by referring to its key name, inside square brackets: Example Get the value of the “model” key:thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964}x = thisdict[“model”] Output:Mustang There is also a method called get() that will give you the same result: Example Get the value of the “model” key:x = thisdict.get(“model”) Output:Mustang Get… Read More »

14. Dictionaries

thisdict = {  “brand”: “Ford”,  “model”: “Mustang”,  “year”: 1964} Dictionary Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. Dictionaries are written with curly brackets, and have keys and values: Example Create… Read More »

13.6 Set Methods

Python has a set of built-in methods that you can use on sets. Example Add an element to the fruits set:fruits = {“apple”, “banana”, “cherry”}fruits.add(“orange”)print(fruits) Output:{‘orange’, ‘banana’, ‘cherry’, ‘apple’} Method Syntax Description add() set.add(elmnt) Adds an element to the set clear() set.clear() Removes all the elements from the set copy() set.copy() Returns a copy of the set difference() set.difference(set) Returns… Read More »

13.5 Join Sets

Join Two Sets There are several ways to join two or more sets in Python. You can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another: Example The union() method returns a new set with all items from both sets:set1 =… Read More »

13.4 Loop Sets

Loop Items You can loop through the set items by using a for loop: Example Loop through the set, and print the values:thisset = {“apple”, “banana”, “cherry”}for x in thisset: print(x) Output:bananacherryapple

13.3 Remove Set Items

Remove Item To remove an item in a set, use the remove(), or the discard() method. Example Remove “banana” by using the remove() method:thisset = {“apple”, “banana”, “cherry”}thisset.remove(“banana”)print(thisset) Output:{‘cherry’, ‘apple’} Note: If the item to remove does not exist, remove() will raise an error. Example Remove “banana” by using the discard() method:thisset = {“apple”, “banana”, “cherry”}thisset.discard(“banana”)print(thisset) Output:{‘cherry’, ‘apple’} Note: If the item to remove does not exist, discard() will NOT raise an error. You… Read More »

12.2 Add Set Items

Add Items Once a set is created, you cannot change its items, but you can add new items. To add one item to a set use the add() method. Example Add an item to a set, using the add() method:thisset = {“apple”, “banana”, “cherry”}thisset.add(“orange”)print(thisset) Output:{‘orange’, ‘apple’, ‘cherry’, ‘banana’} Add Sets To add items from another set into the current set, use… Read More »