Server Side Programming Articles

Page 16 of 2110

Count occurrences of an element in a list in Python

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

In this article we are given a list and a string. We are required to find how many times the given string is present as an element in the list. Python provides several methods to count occurrences, each with different advantages. Using count() Method The count() method is the most straightforward approach. It takes the element as a parameter and returns the number of times it appears in the list ? days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] element = 'Mon' # Given list and element print("Given list:", days) print("Given element:", element) count = ...

Read More

Count occurrences of a character in string in Python

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

We are given a string and a character, and we want to find out how many times the given character is repeated in the string. Python provides several approaches to count character occurrences. Using count() Method The simplest approach is using Python's built-in count() method ? text = "How do you do" char = 'o' print("Given String:", text) print("Given Character:", char) print("Number of occurrences:", text.count(char)) Given String: How do you do Given Character: o Number of occurrences: 4 Using range() and len() We can design a for loop to ...

Read More

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 394 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
Showing 151–160 of 21,091 articles
« Prev 1 14 15 16 17 18 2110 Next »
Advertisements