Found 10784 Articles for Python

How can I convert a Python tuple to string?

Gireesha Devara
Updated on 24-Aug-2023 13:30:09

6K+ Views

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. There are three different ways we can convert a python tuple to string. Using a for loop. Using the python join() method Using the functool.reduce() method Using the for loop In python, we can easily iterate the tuple elements using the for loop, then we will append/add each element to the string object. In this below example we ... Read More

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

Pythonic
Updated on 30-Jul-2019 22:30:21

237 Views

In Python 2.x, both methods are available, but in Python 3.x iteritems() is deprecated. As far as Python 2.x is concerned, items() method of dictionary object returns list of two element tuples, each tuple containing key and value. On the other hand iteritems() is a generator which provides an iterator for items in a dictionary >>> d = {'1': 1, '2': 2, '3': 3} >>> d.items() [(1, 1), (2, 2), (3, 3)] >>> for i in d.iteritems(): print i ('1', 1) ('2', 2) ('3', 3) In Python 3, items() method behaves ... Read More

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

Gireesha Devara
Updated on 24-Aug-2023 13:28:07

622 Views

In Python, using a for loop is common to iterate a single data structure like list, but what if we need to iterate two/multiple lists parallelly?. In this article we will see different ways to iterate two/multiple lists parallelly. By using range() function, we can iterate the two lists parallelly. Lets see the below examples and how we are going to iterate two lists in python. Using the for loop If length of the two lists are same Assuming that both lists have the same length, here we are using the len() method to get the length of ... Read More

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

Gireesha Devara
Updated on 24-Aug-2023 13:24:45

6K+ 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. Let’s discuss how each function will convert the string representation of a dictionary to an original 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. ... Read More

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

Malhar Lathkar
Updated on 16-Jun-2020 11:19:42

244 Views

The best way is to use not operator on list object. If list is empty it returns true, otherwise false.>>> L1=[] >>> not L1 True >>> L1=[1,2] >>> not L1 FalseAnother method is to check if length of list is zero which means it is empty>>> L1=[] >>> len(L1) 0 >>> L1=[1,2] >>> len(L1) 2

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

Gireesha Devara
Updated on 24-Aug-2023 13:17:21

427 Views

Python is being updated regularly with new features and supports. Starting from 1994 to current release, there are lots of updates in Python versions. By 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 24-Aug-2023 13:00:55

567 Views

In Python lists are one the built−in data structure which is used to store collections of data. The lists are mutable, which means we can modify its element after it is created. We have some list methods like append, insert, extend, remove, and clear which are used to change the list content by adding, updating, or removing elements from the list object. In this article, we will discuss how we can update an existing element in the list. Updating the list elements using the index position The list is an index−based sequential data structure, so that we can access the ... 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?

Malhar Lathkar
Updated on 20-Jun-2020 07:39:18

111 Views

It does work. As you have used sleep() method inside the loop, no activity takes place for (0.9*36) seconds. It is not asking for input. After the end of loop, the window will show text field with the given string inside it.

When are python objects candidates for garbage collection?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

191 Views

A python object or variable will be eligible for garbage collection as soon as all references to it go out of scope or are manually deleted (del x). We would have to presume there were no references to the object anywhere else for it to be garbage collected.

Advertisements