Create Dictionary from List in Python

AmitDiwan
Updated on 08-Sep-2021 06:48:26

2K+ Views

When it is required to create dictionary from a list, the ‘fromkeys’ method in the ‘dict’ method is used.ExampleBelow is a demonstration of the same −my_list = ['Hi', 'Will', 'how', 'Python', 'cool'] print("The list is ") print(my_list) my_dict = dict.fromkeys(my_list, "my_value") print(my_dict)OutputThe list is ['Hi', 'Will', 'how', 'Python', 'cool'] {'Hi': 'my_value', 'Will': 'my_value', 'how': 'my_value', 'Python': 'my_value', 'cool': 'my_value'}ExplanationA list of strings is defined and is displayed on the console.The ‘fromkeys’ method present in ‘dict’ is used to convert the elements of the list to dictionary keys.The value for every key is specified here itself.This is assigned to a ... Read More

Remove Dictionary from List if Value is Not Present in Python

AmitDiwan
Updated on 08-Sep-2021 06:46:39

4K+ Views

When it is required to remove dictionary from a list of dictionaries if a particular value is not present, a simple iteration and the ‘del’ operator is used.ExampleBelow is a demonstration of the same −my_list = [{"id" : 1, "data" : "Python"}, {"id" : 2, "data" : "Code"}, {"id" : 3, "data" : "Learn"}] print("The list is :") print(my_list) for index in range(len(my_list)): if my_list[index]['id'] == 2: del my_list[index] break print("The result is :") print(my_list)OutputThe list ... Read More

Remove Tuples with Difference Greater than K in Python

AmitDiwan
Updated on 08-Sep-2021 06:45:23

165 Views

When it is required to remove tuples with difference greater than K, use the abs() method.Below is a demonstration of the same −Examplemy_tuple = [(41, 18), (21,57), (39, 22), (23, 42), (22, 10)] print("The tuple is :") print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1])

Find All Occurrences of Substring in List of Strings

AmitDiwan
Updated on 08-Sep-2021 06:41:51

308 Views

When it is required to get all the occurrences of a substring from the list of strings, a simple list comprehension and the ‘startswith’ method is used.ExampleBelow is a demonstration of the same −my_string = "Is python fun to learn?" print("The list is :") print(my_string) substring = "pyt" print("The substring is :") print(substring) my_result = [i for i in range(len(my_string)) if my_string.startswith(substring, i)] print("The result is :") print(my_result)OutputThe list is : Is python fun to learn? The substring is : pyt The result is : [3]ExplanationA string is defined and is displayed on the console.Another substring ... Read More

Extract Strings with Successive Alphabets in Alphabetical Order in Python

AmitDiwan
Updated on 08-Sep-2021 06:39:12

193 Views

When it is required to extract strings which have successive alphabets in alphabetical order, a simple iteration, and the ‘ord’ method for Unicode representation is used.ExampleBelow is a demonstration of the same −my_list = ["python", 'is', 'cool', 'hi', 'Will', 'How'] print("The list is :") print(my_list) my_result = [] for element in my_list: for index in range(len(element) - 1): if ord(element[index]) == ord(element[index + 1]) - 1: my_result.append(element) break print("The result ... Read More

Extract Words Starting with Vowel from a List in Python

AmitDiwan
Updated on 07-Sep-2021 19:52:11

2K+ Views

When it is required to extract words starting with vowel from a list, a simple iteration, a flag value and the ‘startswith’ method are used.Below is a demonstration of the same −Example:my_list = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is :") print(my_list) my_result = [] my_vowel = "aeiou" print("The vowels are ") print(my_vowel) for index in my_list: my_flag = False for element in my_vowel: if index.startswith(element): my_flag = True ... Read More

Format Date and Time from Milliseconds in Java

Maruthi Krishna
Updated on 07-Sep-2021 13:12:20

2K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object.To format milli seconds to date −Create the format string as dd MMM yyyy HH:mm:ss:SSS Z.The Date class constructor accepts a long value representing the milliseconds as a parameter and creates a date object.Finally format the date object using the format() method.Exampleimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { ... Read More

Print Rows Where All Elements' Frequency is Greater Than K in Python

AmitDiwan
Updated on 07-Sep-2021 12:56:19

191 Views

When it is required to print rows where all its elements’ frequency is greater than K, a method is defined that takes two parameters, and uses ‘all’ operator and iteration to give the result.Below is a demonstration of the same −Exampledef frequency_greater_K(row, K) : return all(row.count(element) > K for element in row) my_list = [[11, 11, 32, 43, 12, 23], [42, 14, 55, 62, 16], [11, 11, 11, 11], [42, 54, 61, 18]] print("The tuple is :") print(my_list) K = 1 print("The value of K is :") print(K) my_result = [row for row in my_list if frequency_greater_K(row, ... Read More

Repeat Elements at Custom Indices in Python

AmitDiwan
Updated on 07-Sep-2021 12:22:36

108 Views

When it is required to repeat elements at custom indices, a simple iteration, enumerate attribute, the ‘extend’ method and the ‘append’ method are used.Below is a demonstration of the same −Examplemy_list = [34, 56, 77, 23, 31, 29, 62, 99] print("The list is :") print(my_list) index_list = [3, 1, 4, 6] my_result = [] for index, element in enumerate(my_list): if index in index_list: my_result.extend([element, element]) else : my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [34, 56, 77, 23, 31, 29, ... Read More

Comparison Between Bluejacking and Bluesnarfing

Pranav Bhardwaj
Updated on 07-Sep-2021 11:53:08

512 Views

Bluetooth is a service that helps in sharing data between multiple devices. We communicate with our computers or any other mobile devices with the help of Bluetooth. Also, it allows us to have a conversation without wires. Nowadays, Bluetooth wireless earphones are there that you can use to listen to music and for discussions. The only problem with these Bluetooth devices is that they have their specific range connected with other devices.BluejackingBluejacking works in a similar way to that of Bluetooth. It is used for sending unlicensed messages to the other Bluetooth device. Bluetooth is the shortrange wireless technology for ... Read More

Advertisements