Pythonic has Published 26 Articles

How to create Python dictionary from list of keys and values?

Pythonic

Pythonic

Updated on 08-Nov-2023 00:37:00

21K+ Views

If L1 and L2 are list objects containing keys and respective values, following methods can be used to construct dictionary object. Zip two lists and convert to dictionary using dict() function >>> L1 = ['a', 'b', 'c', 'd'] >>> L2 = [1, 2, 3, 4] >>> d = dict(zip(L1, L2)) ... Read More

What is "not in" operator in Python?

Pythonic

Pythonic

Updated on 09-Sep-2023 15:12:50

4K+ Views

In Python, 'not in' membership operator evaluates to true if it does not finds a variable in the specified sequence and false otherwise. For example >>> a = 10 >>> b = 4 >>> l1 = [1, 2, 3, 4, 5] >>> a not in l1 True >>> b not ... Read More

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

Pythonic

Pythonic

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

3K+ Views

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, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

How to define a Python dictionary within dictionary?

Pythonic

Pythonic

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

153 Views

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

5K+ Views

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

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

How to iterate over dictionaries using 'for' loops in Python?

Pythonic

Pythonic

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

237 Views

Even though dictionary itself is not an iterable object, the items(), keys() and values methods return iterable view objects which can be used to iterate through dictionary. The items() method returns a list of tuples, each tuple being key and value pair. >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} ... Read More

How to create a Python dictionary from an object's fields?

Pythonic

Pythonic

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

239 Views

The __dict__ attribute returns a dictionary out of fields of any object. Let us define a class person >>> class person: def __init__(self): self.name='foo' self.age = 20 def show(self): print (self.name, self.age) We now declare an object of this ... Read More

How to access Python dictionary in simple way?

Pythonic

Pythonic

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

92 Views

There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value. >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1.get('age') 23 Another way is to use key inside ... Read More

How to map two lists into a dictionary in Python?

Pythonic

Pythonic

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

309 Views

Easiest way is to create a zip object that returns a generator of tuples, each having an item each from two lists. The zip object can then be transformed into a dictionary by using built-in dict() function >>> l1=['name', 'age', 'marks'] >>> l2=['Ravi', 23, 56] >>> z=zip(l1, l2) >>> ... Read More

Advertisements