When it is required to convert a dictionary, that contains pairs of key values into a flat list, dictionary comprehension can be used.It iterates through the dictionary and zips them using the ‘zip’ method.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −Example Live Demofrom itertools import product my_dict = {'month_num' : [1, 2, 3, 4, 5, 6], 'name_of_month' : ['Jan', 'Feb', 'March', 'Apr', 'May', 'June']} print("The dictionary is : ") print(my_dict) my_result = dict(zip(my_dict['month_num'], my_dict['name_of_month'])) print("The flattened dictionary is: ") print(my_result)OutputThe dictionary is ... Read More
When it is required to extract tuples that have a specific number of elements, list comprehension can be used. It iterates over the elements of the list of tuple and puts forth condition that needs to be fulfilled. This will filter out the specific elements and stores them in another variable.Below is a demonstration of the same −Example Live Demomy_list = [(34, 56), (45, 6), (111, 90), (11, 35), (78, )] print("The list is : ") print(my_list) K = 2 print("The value of K has been initialized to" + "str(K)") my_result = [sub for sub in my_list if ... Read More
When it is required to convert a set structure into a tuple, and a tuple into a set, the ‘tuple’ and ‘set’ methods can be used.Below is a demonstration of the same −Example Live Demomy_set = {'ab', 'cd', 'ef', 'g', 'h', 's', 'v'} print("The type is : ") print(type(my_set), " ", my_set) print("Converting a set into a tuple") my_tuple = tuple(my_set) print("The type is : ") print(type(my_tuple), " ", my_tuple) my_tuple = ('ab', 'cd', 'ef', 'g', 'h', 's', 'v') print("The tuple is:") print(my_tuple) print(type(my_tuple), " ", my_tuple) print("Converting tuple to set") my_set = set(my_tuple) print(type(my_set), " ", my_set)OutputThe type is : ... Read More
When it is required to take a string and replace every blank space with a hyphen, the ‘replace’ method can be used. It takes two parameters, the blank space, and the value with which it needs to be replaced (hyphen in this case).Below is a demonstration of the same −Example Live Demomy_string = input("Enter a string :") print("The string entered by user is :") print(my_string) my_string = my_string.replace(' ', '-') print("The modified string:") print(my_string)OutputEnter a string : A-B-C-D E- A-B-C-D E- The string entered by user is : A-B-C-D E- The modified string: A-B-C-D--E-ExplanationAn input string is asked to be entered ... Read More
When it is required to replace all the occurrences of ‘a’ with a character such as ‘$’ in a string, the string can be iterated over and can be replaced using the ‘+=’ operator.Below is a demonstration of the same −Example Live Demomy_str = "Jane Will Rob Harry Fanch Dave Nancy" changed_str = '' for char in range(0, len(my_str)): if(my_str[char] == 'a'): changed_str += '$' else: changed_str += my_str[char] print("The original string is :") print(my_str) print("The modified string is : ") print(changed_str)OutputThe original string is : Jane Will Rob Harry Fanch ... Read More
When it is required to find an element that occurs odd number of times in a list, a method can be defined. This method iterates through the list and checks to see if the elements in the nested loops match. If they do, the counter is incremented. If that count is not divisible by 2, the specific element of the list is returned as the result. Otherwise, -1 is returned as the result.Below is a demonstration of the same −Example Live Demodef odd_occurence(my_list, list_size): for i in range(0, list_size): count = 0 for ... Read More
When it is required to remove a specific occurrence of a given word in a list of words, given that the words can be repeated, a method can be defined, that iterates through the list, and increments the counter by 1. If the count and the specific occurrence match, then the specific element from the list can be deleted.Below is a demonstration of the same −Example Live Demodef remove_word(my_list, my_word, N): count = 0 for i in range(0, len(my_list)): if (my_list[i] == my_word): count = count + 1 ... Read More
To retrieve the OS details of the Azure VM, we need to use the Get-AzVM command.ExampleGet-AzVM -VMName TestMachine2k16When you run the above command, it retrieves the VM TestMachine2k16 information and there is an OSType property which shows that if the VM’s OS is Linux or Windows, or any other type.But when you select the OSType, you won’t get anything. See below.ExamplePS C:\> Get-AzVM -VMName TestMachine2k16 | Select OStype OStype ------Because this property is a part of another property and hence can’t be accessed directly. When you expose the full properties of the VM, you will get the StorageProfile, which ... Read More
To delete the azure resource group using PowerShell, we need to use the Remove-AZResourceGroup command. But before using this command, make sure that no usable resources exist in the resource group that you want to delete.To check if the resources are available in the resource group, use the below command. Here we are using the TestRG resource group name.ExampleGet-AzResource -ResourceGroupName TestRGOnce you are confirmed that you need to delete the Resource Group then use the below command to delete the resource group.ExampleRemove-AzResourceGroup TestRG -Force -VerboseWhen you use the -Force parameter, you won’t be prompted for deletion confirmation.Read More
To export the azure VMs using PowerShell, we first need to get their desired properties. The cmdlet Get-AZVM will get all the VMs connected to the particular subscription. To export them to the CSV file we can use the below command.ExampleGet-AzVM | Export-Csv .\AZVMs.csv -NoTypeInformationOnce you run the above command, you will notice that you get all the properties of the VM, and sometimes they are not needed. To get the particular properties of the VM use the Select-Object (alias Select) command.ExampleGet-AzVM | Select Name, ResourceGroupName, Location, @{N='VMSize';E={$_.HardwareProfile.VmSize}} | Export-Csv .\AzureVms.csv -NoTypeInformationIf you want to export the VMs from a ... Read More