Pythonic has Published 42 Answers

How to pop-up the first element from a Python tuple?

Pythonic

Pythonic

Updated on 20-Feb-2020 11:27:15

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1, 2, 3, 4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, ... Read More

How to define a Python dictionary within dictionary?

Pythonic

Pythonic

Updated on 18-Feb-2020 07:38:26

A dictionary object is mutable. Hence one dictionary object can be used as a value component of a key. So we can create a nested dictionary object another dictionary object is defined as value associated with key.>>>> students={"student1":{"name":"Raaj", "age":23, "subjects":["Phy", "Che", "maths"], "GPA":8.5}, "student2":{"name":"Kiran", "age":21, "subjects":["Phy", "Che", "bio"], "GPA":8.25}}Read More

How to iterate through a tuple in Python?

Pythonic

Pythonic

Updated on 19-Dec-2019 09:06:37

There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −for var in tuple: stmt1 stmt2ExampleFollowing script will print all items in the ... Read More

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

Pythonic

Pythonic

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

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 ... Read More

How can I convert a Python tuple to string?

Pythonic

Pythonic

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

To convert to tuple of string objects into a single string, you can use join() function. Ensure that trget string is initialised to null string >>> T1='1','2','3' >>> s=''.join(T1) >>> s '123'

How to clone or copy a list in Python?

Pythonic

Pythonic

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

In Python, assignment operator doesn’t create a new object, rather it gives another name to already existing object. This can be verified by id() function >>> L1 = [1, 2, 3, 4] >>> L2 = L1 >>> id(L1) 185117137928 >>> id(L2) 185117137928 To actually copy a list, following ... Read More

How to flatten a shallow list in Python?

Pythonic

Pythonic

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

A simple and straightforward solution is to append items from sublists in a flat list using two nested for loops. lst = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] flatlist = [] for sublist in lst: for item in sublist: ... Read More

What are different Identity operators types in Python?

Pythonic

Pythonic

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

Each Python object is assigned a unique identification number when it is stored in memory. It can be fetched by id() function. The is operator compares id() of two objects and returns True if both objects have same value otherwise it returns false. The is not operator on the other ... Read More

What are different assignment operators types in Python?

Pythonic

Pythonic

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

Following table shows all assignment operators − Operator Description Example = Assigns values from right side operands to left side operand c = a + b value of a + b into c += It adds right operand to the left operand and ... Read More

What are different bitwise operators types in Python?

Pythonic

Pythonic

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

Bitwise operators operate upon bits as operands. Following bitwise operators are defined in Python − & (bitwise AND): returns 1 if both bit operands are 1 | (bitwise OR): returns 1 even if one of two bit operands is 1 ^ (bitwise XOR): returns 1 only if one operand ... Read More

Advertisements