Malhar Lathkar has Published 50 Articles

How we can iterate through a Python list of tuples?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:07:37

7K+ Views

Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=’ ‘ to print all items in a tuple in one line. Another print() introduces new line after each tuple.ExampleL=[(1, 2, 3), (4, 5, ... Read More

What are Reserved Keywords in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 14-Feb-2020 11:03:21

19K+ Views

Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can’t be used as identifiers for other programming elements like name of variable, function etc.Following is the list of reserved keywords in Python ... Read More

What is the best way to handle list empty exception in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 19-Dec-2019 09:26:52

2K+ Views

List is an ordered sequence of elements. Individual element in list is accessed using index starting with 0 and goes up to length-1. If index goes beyond this range, IndexError exception is encountered.In following example, an infinite loop is used to pop one element at a time. As loop tries ... Read More

How to get length of a list of lists in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 19-Dec-2019 09:23:22

230 Views

You can employ a nested loop to count number of elements in each sublist of a list>>> a=[[1, 2, 3], [4, 5, 6]] >>> c=0 >>> for x in a:       for y in x:       c=c+1 >>> c 6

How to recursively iterate a nested Python dictionary?

Malhar Lathkar

Malhar Lathkar

Updated on 17-Dec-2019 06:25:56

8K+ Views

Given below is a nested directory objectD1={1: {2: {3: 4, 5: 6}, 3: {4: 5, 6: 7}}, 2: {3: {4: 5}, 4: {6: 7}}}ExampleFollowing recursive function is called repetitively if the value component of each item in directory is a directory itself.def iterdict(d):   for k, v in d.items():   ... Read More

Why is indentation important in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 30-Sep-2019 09:00:06

685 Views

Many a times it is required to treat more than one statements in a program as a block. Different programming languages use different techniques to define scope and extent of block of statements in constructs like class, function, conditional and loop. In C and C++ for example, statements inside curly ... Read More

How can import python module in IDE environment available in tutorialspoint?

Malhar Lathkar

Malhar Lathkar

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

548 Views

You can import any built-in module while writing Python script using TutorialsPoint's online IDE. For example you can import time, sys, os, re modules. However, the modules that are not bundled with the distribution - the one which needs to be installed using package managers like pip or conda - ... Read More

What's the difference between lists and tuples in Python?

Malhar Lathkar

Malhar Lathkar

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

432 Views

List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type. However, main difference between list and tuple is that list object is mutable whereas tuple object is immutable. Immutable object can not be modified ... Read More

What is the difference between the != and <> operators in Python?

Malhar Lathkar

Malhar Lathkar

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

172 Views

In Python 2.x, both != and operators are available to check if two operands are not equal. Both return true if operands are not equal and false if they are equal.In Python 3.x, operator has been deprecated.

How to exit from a Python if clause?

Malhar Lathkar

Malhar Lathkar

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

462 Views

It is not possible to exit from an if block of Python code. The break keyword does appear in if block but it has to inside a loop. It is however possible to exit from entire program from inside if block by sys.exit()

Advertisements