Python
- 09 April
TypeError: ‘dict_values’ Object Is Not Subscriptable
Reproducing TypeError: ‘dict_values object is not subscriptable When you try to access the dict_values object through index, the error will raise stating TypeError: 'dict_values' object is not subscriptable. Here is the example below. [crayon-678d9ce427571582748095/] [crayon-678d9ce427578676817807/] Convert dict_values object to list To resolve TypeError: 'dict_values object is not subscriptable, convert dict_values object to list before accessing […]
- 17 March
Python Sleep Milliseconds(ms) with examples
💡 Outline You can use time.sleep() method to sleep for milliseconds in Python. If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method. [crayon-678d9ce427d11508767228/] 1. Introduction Sometimes, the execution of a program needs to be suspended or paused for a given time. This is […]
- 13 March
Create Empty Array in Python
Creating an Empty Array in Python Before moving towards various solutions for creating an empty array in Python, we must understand what an empty array means. It means a collection of items with a particular data type containing no items or elements. We can use different ways to create an empty one-dimensional, two-dimensional and multi-dimensional […]
- 11 March
Remove Non-alphanumeric Characters in Python
1. Overview Non-alphanumeric characters are those that are not letters or numbers, such as punctuation marks, symbols, spaces, or special characters. Sometimes, we may want to remove these characters from String in Python, for example, to clean user input, extract data etc. 2. Introduction to Problem Let’s say we have a string: [crayon-678d9ce428873065120815/] As we […]
- 08 March
Read File without Newline in Python
Read File without NewLine in Python In Python, there are multiple ways to read a file without new lines, but before diving into those methods, let’s look at the .txt file we will use for this article. [crayon-678d9ce428df2678332436/] You can follow along using the file.txt or your own file. Use read() and replace() Methods To […]
- 07 March
Print Without Space in Python
Using the sep Parameter Use the sep parameter to print without space in Python. [crayon-678d9ce429259440780546/] [crayon-678d9ce42925e945817564/] Python’s print() method displays a specified message or variables’ value on the screen or other standard output devices. Here, the message means a string or an object or object that will be transformed into a string before writing to […]
- 06 March
Check if Variable is Empty in Python
1. Introduction Checking if a variable is empty in Python is a fundamental task, especially in data validation or conditional logic. For example, in a user registration form, checking if the username field is empty is crucial before processing the form. Our goal to determine whether a variable like username contains any data or is […]
- 28 February
Convert List to Integer in Python
Using for Loop To convert the entire list into one integer in Python: Create a list having integer-type elements. Declare and initialize a variable with 0; it will be used to hold the final integer value. Use the for loop to iterate over the list items. In each iteration: multiply the variable created in the […]
- 20 February
Get First n Elements of List in Python
Using List slicing Use list slicing to get first n elements of List in Python. Here’s an example: [crayon-678d9ce429fb6797690803/] [crayon-678d9ce429fbb885765047/] In the above example, list slicing is used to get a list’s first n elements. As we can see, a list was defined as my_list with 6 elements [10, 20, 30, 40, 50, 60]. And […]