Found 33676 Articles for Programming

Create a list of tuples from given list having number and its cube in each tuple using Python

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

889 Views

When it is required to create a list from a given list that have a number and its cube, the list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [32, 54, 47, 89] print("The list is ") print(my_list) my_result = [(val, pow(val, 3)) for val in my_list] print("The result is ") print(my_result)OutputThe list is [32, 54, 47, 89] The result is [(32, 32768), (54, 157464), (47, 103823), (89, 704969)]ExplanationA list is defined, and is displayed on the console.The list comprehension is used to iterate through the list, and use the ‘pow’ method to get ... Read More

Maximum and Minimum K elements in Tuple using Python

AmitDiwan
Updated on 17-Apr-2021 12:55:05

1K+ Views

When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.Below is the demonstration of the same −Example Live Demomy_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result = [] my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp):    if idx < K or idx >= len(temp) - K:       ... Read More

Python program to Find the size of a Tuple

AmitDiwan
Updated on 17-Apr-2021 12:54:22

373 Views

When it is required to find the size of a tuple, the ‘sizeof’ method can be used.Below is the demonstration of the same −Example Live Demoimport sys tuple_1 = ("A", 1, "B", 2, "C", 3) tuple_2 = ("Java", "Lee", "Code", "Mark", "John") tuple_3 = ((1, "Bill"), ( 2, "Ant"), (3, "Fox"), (4, "Cheetah")) print("The first tuple is :") print(tuple_1) print("The second tuple is :") print(tuple_2) print("The third tuple is :") print(tuple_3) print("Size of first tuple is : " + str(sys.getsizeof(tuple_1)) + " bytes") print("Size of second tuple is : " + str(sys.getsizeof(tuple_2)) + " bytes") print("Size of third tuple is: ... Read More

Keys associated with Values in Dictionary in Python

AmitDiwan
Updated on 17-Apr-2021 12:48:00

428 Views

When it is required to find the keys associated with specific values in a dictionary, the ‘index’ method can be used.Below is the demonstration of the same −Example Live Demomy_dict ={"Hi":100, "there":121, "Mark":189} print("The dictionary is :") print(my_dict) dict_key = list(my_dict.keys()) print("The keys in the dictionary are :") print(dict_key) dict_val = list(my_dict.values()) print("The values in the dictionary are :") print(dict_val) my_position = dict_val.index(100) print("The value at position 100 is : ") print(dict_key[my_position]) my_position = dict_val.index(189) print("The value at position 189 is") print(dict_key[my_position])OutputThe dictionary is : {'Hi': 100, 'there': 121, 'Mark': 189} The keys in the dictionary are : ... Read More

Python dictionary, set and counter to check if frequencies can become same

AmitDiwan
Updated on 17-Apr-2021 12:47:20

351 Views

When it is required to check if the frequency of a dictionary, set and counter are same, the Counter package is imported and the input is converted into a ‘Counter’. The values of a dictionary are converted to a ‘set’ and then to a list. Based on the length of the input, the output is displayed on the console.Below is the demonstration of the same −Example Live Demofrom collections import Counter def check_all_same(my_input):    my_dict = Counter(my_input)    input_2 = list(set(my_dict.values()))    if len(input_2)>2:       print('The frequencies are not same')    elif len (input_2)==2 and input_2[1]-input_2[0]>1:       ... Read More

Python counter and dictionary intersection example

AmitDiwan
Updated on 17-Apr-2021 12:46:54

314 Views

When it is required to demonstrate a counter and dictionary intersection, the Counter and dictionary can be used.Below is the demonstration of the same −Example Live Demofrom collections import Counter def make_string(str_1, str_2):    dict_one = Counter(str_1)    dict_two = Counter(str_2)    result = dict_one & dict_two    return result == dict_one string_1 = 'Hi Mark' string_2 = 'how are yoU' print("The first string is :") print(string_1) print("The second string is :") print(string_2) if (make_string(string_1, string_2)==True):    print("It is possible") else:    print("It is not possible")OutputThe first string is : Hi Mark The second string is : how ... Read More

Python dictionary with keys having multiple inputs

AmitDiwan
Updated on 17-Apr-2021 12:46:29

1K+ Views

When it is required to create a dictionary where keys have multiple inputs, an empty dictionary can be created, and the value for a specific key can be specified.Below is the demonstration of the same −Example Live Demomy_dict = {} a, b, c = 15, 26, 38 my_dict[a, b, c] = a + b - c a, b, c = 5, 4, 11 my_dict[a, b, c] = a + b - c print("The dictionary is :") print(my_dict)OutputThe dictionary is : {(15, 26, 38): 3, (5, 4, 11): -2}ExplanationThe required packages are imported.An empty dictionary is defined.The values for ... Read More

Sort Dictionary key and values List in Python

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

475 Views

When it is required to sort the key and values in a dictionary, the ‘sorted’ method can be used.Below is the demonstration of the same −Example Live Demomy_dict = {'Hi': [1, 6, 3],    'there': [2, 9, 6],    'Mark': [16, 7]} print("The dictionary is : ") print(my_dict) my_result = dict() for key in sorted(my_dict):    my_result[key] = sorted(my_dict[key]) print("The sorted dictionary is : " ) print(my_result)OutputThe dictionary is : {'Hi': [1, 6, 3], 'there': [2, 9, 6], 'Mark': [16, 7]} The sorted dictionary is : {'Hi': [1, 3, 6], 'Mark': [7, 16], 'there': [2, 6, 9]}ExplanationA dictionary ... Read More

Sort Python Dictionaries by Key or Value

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

222 Views

When it is required to sort the dictionaries in Python using a key or a value, a dictionary can be defined, and key value pairs can be inserted into it. A ‘for’ loop can be used to iterate through the key value pair, and sort it using ‘sort’ method. This method can be called.Below is the demonstration of the same −Exampledef my_dict(): my_key_value_pair ={} my_key_value_pair[2] = 56 my_key_value_pair[1] = 2 my_key_value_pair[5] = 12 my_key_value_pair[4] = 24 my_key_value_pair[6] = 18 my_key_value_pair[3] = 323 print ("The keys and values after sorting in alphabetical order based on the key are ... Read More

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

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

704 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

Advertisements