Found 26504 Articles for Server Side Programming

Check order of character in string using OrderedDict( ) in Python

AmitDiwan
Updated on 17-Apr-2021 12:40:18

702 Views

When it is required to check the order of the character in the string, the ‘OrderedDict’ method can be used.Below is the demonstration of the same −Example Live Demofrom collections import OrderedDict def check_order(my_input, my_pattern):    my_dict = OrderedDict.fromkeys(my_input)    pattern_length = 0    for key, value in my_dict.items():       if (key == my_pattern[pattern_length]):          pattern_length = pattern_length + 1       if (pattern_length == (len(my_pattern))):          return 'The order of pattern is correct'    return 'The order of pattern is incorrect' my_input = 'Hi Mark' input_pattern = 'Ma' ... Read More

Insertion at the beginning in OrderedDict using Python

AmitDiwan
Updated on 17-Apr-2021 12:39:17

528 Views

When it is required to insert the elements at the beginning of an ordered dictionary, the ‘update’ method can be used.Below is the demonstration of the same −Example Live Demofrom collections import OrderedDict my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) print("The dictionary is :") print(my_ordered_dict) my_ordered_dict.update({'Mark':'7'}) my_ordered_dict.move_to_end('Mark', last = False) print("The resultant dictionary is : ") print(my_ordered_dict)OutputThe dictionary is : OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) The resultant dictionary is : OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])ExplanationThe required packages are imported.An ordered dictionary is created using OrderedDict’.It is displayed on the console.The ‘update’ method is used ... Read More

Ways to sort list of dictionaries by values in Python Using lambda function

AmitDiwan
Updated on 17-Apr-2021 12:38:16

1K+ Views

When it is required to sort the list of dictionaries based on values, the lambda function can be used.Below is the demonstration of the same −Example Live Demofrom operator import itemgetter my_list = [{ "name" : "Will", "age" : 56}, { "name" : "Rob", "age" : 20 }, { "name" : "Mark" , "age" : 34 }, { "name" : "John" , "age" : 24 }] print("The list sorted ... Read More

Ways to sort list of dictionaries by values in Python Using itemgetter

AmitDiwan
Updated on 17-Apr-2021 12:38:42

696 Views

When it is required to sort the list of dictionary based on values, the itemgetter attribute can be used.Below is the demonstration of the same −Example Live Demofrom operator import itemgetter my_list = [{ "name" : "Will", "age" : 56}, { "name" : "Rob", "age" : 20 }, { "name" : "Mark" , "age" : 34 }, { "name" : "John" , "age" : 24 }] print("The list sorted ... Read More

Extract Unique dictionary values in Python Program

AmitDiwan
Updated on 17-Apr-2021 12:09:31

1K+ Views

When it is required to extract unique values from a dictionary, a dictionary is created, and the ‘sorted’ method and dictionary comprehension is used.Below is a demonstration for the same −Example Live Demomy_dict = {'hi' : [5, 3, 8, 0],    'there' : [22, 51, 63, 77],    'how' : [7, 0, 22],    'are' : [12, 11, 45],    'you' : [56, 31, 89, 90]} print("The dictionary is : ") print(my_dict) my_result = list(sorted({elem for val in my_dict.values() for elem in val})) print("The unique values are : ") print(my_result)OutputThe dictionary is : {'hi': [5, 3, 8, 0], ... Read More

Count Number of Lowercase Characters in a String in Python Program

AmitDiwan
Updated on 17-Apr-2021 12:08:56

2K+ Views

When it is required to count the number of lower case characters in a string, the ‘islower’ method and a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" print("The string is ") print(my_string) my_counter=0 for i in my_string:    if(i.islower()):       my_counter=my_counter+1 print("The number of lowercase characters in the string are :") print(my_counter)OutputThe string is Hi there how are you The number of lowercase characters in the string are : 15ExplanationA string is defined and is displayed on the console.A counter value is initialized to ... Read More

Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program

AmitDiwan
Updated on 17-Apr-2021 12:08:37

802 Views

When it is required to take two strings and display the larger string without using any built-in function, the counter can be used to get the length of the strings, and ‘if’ condition can be used to compare their lengths.Below is the demonstration of the same −Example Live Demostring_1= "Hi there" string_2= "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1:    count_1=count_1+1 for j in string_2:    count_2=count_2+1 if(count_1

Python Program to Find All Connected Components using DFS in an Undirected Graph

AmitDiwan
Updated on 17-Apr-2021 12:08:17

2K+ Views

When it is required to find all the connected components using depth first search in an undirected graph, a class is defined that contains methods to initialize values, perform depth first search traversal, find the connected components, add nodes to the graph and so on. The instance of the class can be created and the methods can be accessed and operations and be performed on it.Below is a demonstration of the same −Example Live Democlass Graph_struct:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utililty(self, temp, ... Read More

Python Program to Calculate the Length of a String Without Using a Library Function

AmitDiwan
Updated on 17-Apr-2021 12:07:51

3K+ Views

When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered.Below is the demonstration of the same −Example Live Demomy_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string:    my_counter=my_counter+1 print("The length of the string is ") print(my_counter)OutputThe string is : Hi Will The length of the string is 7ExplanationA string is defined, and is displayed on the console.A counter is initialized to 0.The string is iterated over, and after every element is iterated over, the counter ... Read More

Python Program to Remove the nth Index Character from a Non-Empty String

AmitDiwan
Updated on 17-Apr-2021 12:07:12

839 Views

When it is required to remove a specific index character from a string which isn’t empty, it can be iterated over, and when the index doesn’t match, that character can be stored in another string.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" print("The string is :") print(my_string) index_removed = 2 changed_string = '' for char in range(0, len(my_string)):    if(char != index_removed):       changed_string += my_string[char] print("The string after removing ", index_removed, "nd character is : ") print(changed_string)OutputThe string is : Hi there how are you The ... Read More

Advertisements