Found 10419 Articles for Python

What is the difference between dict.items() and dict.iteritems() in Python?

Akshitha Mote
Updated on 30-Apr-2025 14:24:04

402 Views

In Python, the dict.items() and dict.iteritems() methods are used to return a dictionary. The only difference is that the dict.items() returns the keys and value pairs in the form of a list of tuple pairs, whereas the dict.iteritems() returns an iterator over the dictionary’s (key, value) tuple pairs. The dict.items() and dict.iteritems() functions are present in Python 2.x versions. The dict.iteritems() function is omitted from the Python 3.x version. Example - 'dict.items()' in Python 2.x version Following is an execution of the dict.items() in Python 2.x version - my_dict={1:'one', 2:'two', 3:'three', 4:'four'} print(my_dict.items()) Output Following is the output ... Read More

How can I iterate through two lists in parallel in Python?

Gireesha Devara
Updated on 29-May-2025 10:56:53

788 Views

In Python, using a for loop is common to iterate a single data structure like a list, but if we need to iterate two/multiple lists in parallel, we need to use the range() function. Iterating two Lists Using for Loop Assuming that both lists have the same length, here we are using the len() method to get the length of the list object. Example In the following example, we have iterated through the lists l1 and l2 of same size using for loop - l1 = ['a', 'b', 'c', 'd', 'e'] l2 = [97, 98, 99, 100, 101] length = ... Read More

How to convert a String representation of a Dictionary to a dictionary in Python?

Gireesha Devara
Updated on 29-May-2025 11:00:02

7K+ Views

To convert a string represented dictionary to an original dictionary, we can use built-in functions like eval(), json.load(), and ast.literal_eval(). Initially, we must ensure that the string contains a valid representation of the dictionary. Using the eval() function The eval() is a Python built-In function that takes a string argument, parses it as a code expression, and evaluates the expression. If the input expression contains a string representation of a dictionary, then the eval() method returns it as a normal Python dictionary. Syntax Following is the syntax of the eval() function in Python - eval(expression[, globals[, locals]]) Example ... Read More

What is best way to check if a list is empty in Python?

Akshitha Mote
Updated on 29-May-2025 14:05:10

428 Views

A List in Python is a mutable sequence of elements separated by commas ", ". It is represented by square braces "[ ]".An empty list contains no elements, meaning its length is 0. It is indicated by the square braces with no elements in it.In this article, we will explore various methods to check whether a given list in Python is empty.Checking for an Empty List using Not Operator In Python, the not operator is one of the logical operators. If the given condition is True, it will return False and vice versa. This operator evaluates the empty list as False. The 'not' ... Read More

How do I check what version of Python is running my script?

Gireesha Devara
Updated on 29-May-2025 11:09:11

696 Views

Python is being updated regularly with new features and support. Starting from 1994 to the current release, there have been lots of updates in Python versions.Using Python standard libraries like sys or platform modules, we can get the version information of Python that is actually running on our script. In general, the Python version is displayed automatically on the console immediately after starting the interpreter from the command line. Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. Using the version attribute The sys ... Read More

How we can update a Python list element value?

Gireesha Devara
Updated on 29-May-2025 10:11:23

998 Views

In Python, lists are one of the built-in data structures that are used to store collections of data. The lists are mutable, which means we can modify their elements after they are created.Updating Python List Elements You can update single or multiple list elements using the append, insert, extend, remove, and clear functions. In this article, we will discuss how we can update an existing element in the list. The list is an index-based sequential data structure. We can access the list elements by their index position, known as index values. The index values of the Python lists are represented ... Read More

How we can update a Python tuple element value?

Gireesha Devara
Updated on 24-Aug-2023 15:52:50

1K+ Views

Python tuple is an immutable object, which means once tuple is created you cannot change, update, add, or remove its values. If we try to update any of its elements, then it will throw the TypeError. In this article, we will use three different ways like list conversion, slicing, packing, and unpacking to modify a tuple without raising any errors. Using list conversion By converting tuple to a list then converting it back, we can update the values in tuple, but this will create a list copy of tuple in memory. Example After converting the tuple to list, we ... Read More

Python3 - Why loop doesn\'t work?

Akshitha Mote
Updated on 29-May-2025 15:56:17

177 Views

In Python, using loops, we can execute a statement or a group of statements multiple times. Usually, the loop works for all the conditions, but there are certain scenarios where the functionality of loops is affected. In this article, we will understand those scenarios with examples. When sleep() method is used inside Loop The sleep() method is defined in the time module and used to suspend or stop the execution of the program for a specified number of seconds. When the sleep() method is placed inside a loop, it suspends the execution of each iteration for the given time. After ... Read More

How to create Python objects based on an XML file?

Gireesha Devara
Updated on 24-Aug-2023 12:45:36

2K+ Views

XML (Extensible Markup Language), which is a markup−language that is used to structure, store, and transfer data between systems. At some point we need to read/write the XML data using the Python language. By using the untangle library we can create Python objects based on an XML file. The untangle is a small Python library which converts an XML document to a Python object. The untangle has a very simple API. We just need to call the parse() function to get a Python object. Syntax untangle.parse(filename, **parser_features) Parameters Filename: it can be a XML string, a XML filename, ... Read More

Do you think garbage collector can track all the Python objects?

Akshitha Mote
Updated on 29-May-2025 13:07:10

171 Views

The garbage collector doesn't track (and collect) all the objects; it can only track objects that are unreachable (have a reference count of zero), and the objects that are involved in circular references. What is a garbage collector? The garbage collector is an automatic (implicit) process that handles memory allocation and deallocation, ensuring efficient use of memory. We can interact with the garbage collector in Python explicitly and modify its behavior, using the gc module; it is a built-in module in Python for garbage collection.  By default, it is turned ON; you may turn it off if you are sure ... Read More

Advertisements