Programming Articles - Page 3249 of 3366

How to flatten a shallow list in Python?

Gireesha Devara
Updated on 29-May-2025 10:37:14

2K+ Views

Flattening a shallow list means converting a nested list into a simple, single-dimensional list. In other words, converting a multidimensional list into a one-dimensional list. Flattening can be performed using different techniques like nested for loops, list comprehensions, list concatenation, and using built-in functions. In this article, we will discuss a few techniques to flatten a shallow Python list. Flattening a shallow list using a nested for loop By using a nested for loop and a list.append() method, we can flatten the shallow list. Let’s have a look and see how this can be done in a program. Example This simple example ... Read More

How to clone or copy a list in Python?

Gireesha Devara
Updated on 29-May-2025 10:40:20

2K+ Views

The list in Python is a sequence data type that is used to store various types of data. A list is created by placing each data element inside square brackets "[]" and these are separated by commas. In Python, the assignment operator doesn’t create a new object; rather, it gives another name to an already existing object. This can be verified by id() function >>> L1 = [1, 2, 3, 4] >>> L2 = L1 >>> id(L1) 185117137928 >>> id(L2) 185117137928 There are various ways of cloning/copying a list in Python. In this article, we will discuss some ... Read More

How can I convert a Python tuple to string?

Gireesha Devara
Updated on 29-May-2025 10:44:05

11K+ Views

A tuple is a collection of objects that is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are that tuples cannot be changed, unlike lists, and tuples use parentheses, whereas lists use square brackets. Converting a Python tuple to a StringThere are three different ways we can convert a Python tuple to a string. Using a for loop. Using the Python join() method ... Read More

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

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

503 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

871 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

8K+ 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

471 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

774 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

1K+ 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

Advertisements