Python Articles

Page 16 of 855

Count number of items in a dictionary value that is a list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

Sometimes we have a dictionary where some values are lists and we need to count the total number of items across all these list values. Python provides several approaches to achieve this using isinstance() to check if a value is a list. Using isinstance() with Dictionary Keys We can iterate through dictionary keys and use isinstance() to identify list values ? # defining the dictionary data_dict = {'Days': ["Mon", "Tue", "Wed", "Thu"], 'time': "2 pm", 'Subjects':["Phy", "Chem", "Maths", "Bio"] } print("Given dictionary:", data_dict) ...

Read More

Converting list string to dictionary in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

Sometimes you need to convert a string that looks like a list with key-value pairs into an actual Python dictionary. For example, converting '[Mon:3, Tue:5, Fri:11]' into {'Mon': '3', 'Tue': '5', 'Fri': '11'}. Python provides several approaches to handle this conversion. Using split() and Dictionary Comprehension This approach uses split() to separate elements and slicing to remove the brackets, then creates a dictionary using comprehension ? string_data = '[Mon:3, Tue:5, Fri:11]' # Given string print("Given string:", string_data) print("Type:", type(string_data)) # Using split and slicing result = {sub.split(":")[0]: sub.split(":")[1] for sub in string_data[1:-1].split(", ")} ...

Read More

Converting all strings in list to integers in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

Sometimes we have a list containing strings that represent numbers. In such cases, we need to convert these string elements into actual integers for mathematical operations or data processing. Using List Comprehension with int() The most Pythonic approach uses list comprehension with the int() function to iterate through each element and convert it ? string_numbers = ['5', '2', '-43', '23'] # Given list print("Given list with strings:") print(string_numbers) # Using list comprehension with int() integers = [int(i) for i in string_numbers] # Result print("The converted list with integers:") print(integers) Given ...

Read More

Convert two lists into a dictionary in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 5K+ Views

Python dictionaries store data as key-value pairs, while lists contain a series of values. Converting two lists into a dictionary is a common task where one list provides keys and another provides corresponding values. Python offers several methods to achieve this conversion. Using zip() Function The zip() function is the most pythonic and efficient way to combine two lists into a dictionary. It pairs elements from both lists and creates key-value pairs ? keys = ["Mon", "Tue", "Wed"] values = [3, 6, 5] # Given lists print("Keys:", keys) print("Values:", values) # Convert to dictionary ...

Read More

Convert string to DateTime and vice-versa in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 295 Views

Python has extensive date and time manipulation capabilities. In this article, we'll see how a string with proper format can be converted to a datetime object and vice versa. Converting String to DateTime with strptime() The strptime() function from the datetime module can convert a string to datetime by taking appropriate format specifiers ? import datetime dt_str = 'September 19 2019 21:02:23 PM' # Given date time string print("Given date time:", dt_str) print("Data Type:", type(dt_str)) # Define format specifiers dtformat = '%B %d %Y %H:%M:%S %p' # Convert string to datetime datetime_val ...

Read More

Convert set into a list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 395 Views

Converting a set into a list is a common task in Python data analysis. Since sets are unordered collections and lists are ordered, this conversion allows you to access elements by index and maintain a specific order. Using list() Function The most straightforward approach is using the built−in list() function, which directly converts a set into a list ? setA = {'Mon', 'day', '7pm'} # Given Set print("Given set:", setA) # Convert set to list result = list(setA) # Result print("Final list:", result) Given set: {'7pm', 'day', 'Mon'} Final list: ...

Read More

Convert number to list of integers in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 11K+ Views

Converting a number to a list of its individual digits is a common task in Python programming. This article explores two efficient approaches: list comprehension and the map() function with str(). Using List Comprehension List comprehension provides a concise way to convert each digit by first converting the number to a string, then iterating through each character and converting it back to an integer ? Example number = 1342 # Given number print("Given number:", number) # Convert to list of digits using list comprehension digits_list = [int(digit) for digit in str(number)] # ...

Read More

Convert list of tuples into digits in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 245 Views

Python has a wide variety of data manipulation capabilities. We have a scenario in which we are given a list which has elements which are pair of numbers as tuples. In this article we will see how to extract the unique digits from the elements of a list which are tuples. Using Regular Expression and Set We can use regular expression module and its function called sub. It is used to replace a string that matches a regular expression instead of perfect match. So we design a regular expression to convert the tuples into normal strings and then ...

Read More

Convert list of strings and characters to list of characters in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 517 Views

When working with lists, we may come across a situation where we have to process a string and get its individual characters for further processing. In this article we will see various ways to convert a list of strings and characters to a list of individual characters. Using List Comprehension We design a nested loop using list comprehension to go through each element of the list and another loop inside this to pick each character from the element which is a string ? days = ['Mon', 'd', 'ay'] # Given list print("Given list :", days) ...

Read More

Convert dictionary object into string in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 410 Views

In Python data manipulation, you may need to convert a dictionary object into a string. This can be achieved using two main approaches: the built-in str() function and the json.dumps() method. Using str() The most straightforward method is to apply str() by passing the dictionary as a parameter. This preserves Python's dictionary representation with single quotes ? Example schedule = {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"} print("Given dictionary:", schedule) print("Type:", type(schedule)) # Using str() result = str(schedule) print("Result as string:", result) print("Type of Result:", type(result)) The output of the ...

Read More
Showing 151–160 of 8,547 articles
« Prev 1 14 15 16 17 18 855 Next »
Advertisements