Pythonic has Published 35 Articles

What is "not in" operator in Python?

Pythonic

Pythonic

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

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

What is "is not" operator in Python?

Pythonic

Pythonic

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

In Python is not membership operator evaluates to false if the variables on either side of the operator point to the same object and true otherwise. For example − >>> a = 10 >>> b = 20 >>> id(a), id(b) (1581561184, 1581561504) >>> a is not b True ... Read More

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

Pythonic

Pythonic

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

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

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

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

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

How do I check whether a file exists using Python?

Pythonic

Pythonic

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

Presence of a certain file in the computer can be verified by two ways using Python code. One way is using isfile() function of os.path module. The function returns true if file at specified path exists, otherwise it returns false. >>> import os >>> os.path.isfile("d:\Package1\package1\fibo.py") True >>> os.path.isfile("d:/Package1/package1/fibo.py") True ... Read More

What is the difference between __str__ and __repr__ in Python?

Pythonic

Pythonic

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

The built-in functions repr() and str() respectively call object.__repr__(self) and object.__str__(self) methods. First function computes official representation of the object, while second returns informal representation of the object. Result of both functions is same for integer object. >>> x = 1 >>> repr(x) '1' >>> str(x) '1' ... Read More

In Python how to create dictionary from two lists?

Pythonic

Pythonic

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

If L1 and L2 are list objects containing keys and respective values, following list comprehension syntax can be used to construct dictionary object. >>> L1 = ['a','b','c','d'] >>> L2 = [1,2,3,4] >>> d = {L1[k]:L2[k] for k in range(len(L1))} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

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

Pythonic

Pythonic

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

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

Advertisements