
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 26504 Articles for Server Side Programming

2K+ Views
In Python, both tuples and lists are used to store data in sequence. Python provides built-in functions and operators that allow us to easily add elements of a tuple into a list and elements of a list into a tuple. In this article, we will explain various methods to perform these conversions, along with example codes. Scenario 1 You are given a list and a tuple as input, your task is to add tuple into the list and print the list as output. For example: # Input List and Tuple my_list = [3, 6, 9, 45, 66] my_tup = ... Read More

884 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

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

371 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

423 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

345 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

311 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

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

473 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

218 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