Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Found 8,533 articles
Possible Words using given characters in Python
In this article, we'll see how to find possible words that can be formed using a given set of characters. We'll take a list of reference words and a list of available characters, then determine which words can be made from those characters. The program uses two functions: one to count character frequencies, and another to check if each word can be formed from the available characters. Example Here's how to find words that can be formed from given characters: def count_characters(character): char_count = {} for n ...
Read MoreAccessing all elements at given Python list of indexes
Sometimes we need to access multiple elements from a list at specific index positions. Python provides several efficient approaches to extract elements at given indices from a list. Using List Comprehension The most Pythonic approach uses list comprehension to iterate through the index list and extract corresponding elements ? days = ["Mon", "Tue", "Wed", "Thu", "Fri"] indices = [1, 3, 4] # printing the lists print("Given list: " + str(days)) print("List of indices: " + str(indices)) # use list comprehension result = [days[i] for i in indices] # Get the result print("Result list: ...
Read MorePython - Convert column to separate elements in list of lists
When working with data structures in Python, you often need to reshape lists by separating columns into different elements. This is particularly useful when converting tabular data into different formats or extracting specific column ranges from nested lists. Using List Slicing and Comprehension You can slice lists at specific positions to create a columnar structure. This approach splits each sublist into two parts: elements from index 2 onwards and elements from index 0 to 2 ? Example data = [[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]] print("The given input ...
Read MorePython - Convert a set into dictionary
Python provides lot of flexibility to handle different types of data structures. There may be a need when you have to convert one Data Structure to another for a better use or better analysis of the data. In this article we will see how to convert a Python set to a Python dictionary. Using zip() and dict() The dict() function can be used to take input parameters and convert them to a dictionary. We also use the zip() function to group the keys and values together which finally become the key-value pairs in the dictionary ? Example ...
Read MoreHow to print double quotes with the string variable in Python?
Printing double quotes with string variables can be tricky since double quotes are part of Python's string syntax. This article explores several methods to include double quotes in your printed output. Common Mistakes The following examples show what not to do when trying to print double quotes ? print(" ") print(" " " ") print(""aString"") The output of the above code is ? File "", line 3 print(""aString"") ^ SyntaxError: invalid syntax Method 1: ...
Read MoreBigram formation from given a Python list
A bigram is a pair of consecutive words formed from a sentence. In Python, bigrams are heavily used in text analytics and natural language processing to analyze word patterns and relationships. What is a Bigram? A bigram takes every two consecutive words from a sentence and creates word pairs. For example, from "hello world python", we get bigrams: ("hello", "world") and ("world", "python"). Using enumerate() and split() This approach splits the sentence into words and uses enumerate() to create pairs from consecutive words ? sentences = ['Stop. look left right. go'] print("The given list ...
Read MoreAvoiding quotes while printing strings in Python
When printing strings in Python, we often need to avoid displaying quotes around individual string elements. This is especially useful when working with lists of strings where we want clean, formatted output without the quotation marks that normally appear. Using join() Method The join() method combines list elements into a single string using a specified separator. This eliminates quotes around individual elements ? Example days = ['Mon', 'Tue', 'Wed'] # The given list print("The given list is : " + str(days)) print("The formatted output is : ") print(' ** '.join(days)) The output ...
Read Moreaskopenfile() function in Python Tkinter
The askopenfile() function in Python Tkinter allows users to browse their file system and select a file through a graphical dialog box. This eliminates the need to hardcode file paths and provides a user-friendly way to open files in your applications. Syntax filedialog.askopenfile(mode='r', **options) Parameters The function accepts several optional parameters ? mode ? File opening mode (default is 'r' for read) initialdir ? Initial directory to open filetypes ? Specify allowed file types title ? Dialog window title Basic Example Here's a simple program that opens a file ...
Read MorePython - Get items in sorted order from given dictionary
Python dictionaries contain key-value pairs that are unordered by default. Often, we need to display or process dictionary items in sorted order based on their keys. This article explores different methods to sort dictionary items. Using operator.itemgetter() The operator module provides itemgetter() function which can extract specific elements from tuples. Using itemgetter(0) sorts by keys, while itemgetter(1) sorts by values ? import operator data = {12: 'Mon', 21: 'Tue', 17: 'Wed'} print("Given dictionary:", data) print("Sorted by keys:") for key, value in sorted(data.items(), key=operator.itemgetter(0)): print(key, "->", value) Given ...
Read MoreCollapsible Pane in Tkinter Python
Tkinter is the GUI building library of Python. In this article, we will see how to create a collapsible pane using Tkinter. Collapsible panes are useful when you have a large amount of data to display on a GUI canvas but don't want it to be visible all the time. They can be expanded or collapsed as needed to save screen space. A collapsible pane typically consists of a toggle button and a frame that can be shown or hidden. When collapsed, only the toggle button is visible. When expanded, the frame containing additional widgets becomes visible. Creating ...
Read More