Malhar Lathkar has Published 19 Articles

What are the differences and similarities between tuples and lists in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:21:13

960 Views

Both 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.SimilaritiesConcatenation, repetition, indexing and slicing can be done on objects of both types>>> #list operations >>> L1=[1, 2, 3] >>> L2=[4, 5, 6] >>> ... Read More

How to get the second-to-last element of a list in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:04:00

25K+ Views

Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end.  As we want second to last element in list, use -2 as index.>>> L1=[1,2,3,4,5] >>> print (L1[-2]) 4

How can I get last 4 characters of a string in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:12:13

2K+ Views

The slice operator in Python takes two operands. First operand is the beginning of slice. The index is counted from left by default. A negative operand starts counting from end. Second operand is the index of last character in slice. If omitted, slice goes upto end.We want last four characters. ... Read More

Can we assign a reference to a variable in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:11:01

1K+ Views

Concept of variable in Python is different from C/C++. In C/C++, variable is a named location in memory. Even if value of one is assigned to another, it creates a copy in another location.int x=5; int y=x;For example in C++, the & operator returns address of the declared variable.coutRead More

How can we unpack a string of integers to complex numbers in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:09:49

211 Views

A string contains two integers separated by comma. It is first split in a list of two strings having digits.>>> s="1, 2".split(", ") >>> s ['1', '2']Two items are then converted to integers and used as arguments for complex() function>>> complex(int(s[0]), int(s[1])) (1+2j)This results in unpacking of string of integers ... Read More

How we can iterate through a Python list of tuples?

Malhar Lathkar

Malhar Lathkar

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

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

331 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

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

Malhar Lathkar

Malhar Lathkar

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

616 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

Advertisements