Found 10784 Articles for Python

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

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

22K+ 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)) >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4} Using dictionary comprehension syntax >>> L1 = ['a','b','c','d'] >>> L2 = [1,2,3,4] >>> d = {k:v for k,v in zip(L1,L2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

In Python how to create dictionary from two lists?

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

316 Views

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 do we define dictionary in Python?

Pythonista
Updated on 25-Feb-2020 11:23:19

134 Views

Dictionary object is an unordered collection of key-value pairs, separated by comma and enclosed in curly brackets. Association of value with key is marked by : symbol between them.>>> D1={'a':1,'b':2,'c':3}Key can appear in a dictionary object only once, whereas single value can be assigned to multiple keys. Key should be of immutable data type i.e. number, string or tuple.>>> D2={1:'aaa', 2:'bbb', 3:'ccc'} >>> D3={(10,10):'x1', (20,20):'x2'} >>> D4={'Microsoft':['MS Office', 'Windows', 'C#'], 'Oracle':['Oracle', 'Java', 'MySQL']}

How can I parse a numeric string to its corresponding float value?

Pythonista
Updated on 25-Feb-2020 11:22:35

81 Views

Python’s number conversion function float() converts an integer to float with fractional part as 0. It also parses a string with valid representation of float number to a float object.>>> float('1.11') 1.11 >>> float(1) 1.0 >>> float('1') 1.0 >>> float('1.1e-2') 0.011

How to convert an integer to a octal string in Python?

Pythonista
Updated on 25-Feb-2020 11:22:01

342 Views

We have use oct() function from Python’s library to convert any integer to its octal equivalent number. We get a string having octal representation>>> oct(100) '0o144' >>> oct(0x10) '0o20' >>> oct(10) '0o12'

How to convert an integer to a hexadecimal string in Python?

Pythonista
Updated on 25-Feb-2020 11:21:28

524 Views

We can use built in hex() function to convert any integer to its hexadecimal representation.>>> hex(100) '0x64' >>> hex(4095) '0xfff' >>> hex(31) '0x1f'

How to convert an integer to an ASCII value in Python?

Pythonista
Updated on 25-Feb-2020 11:16:05

1K+ Views

ASCII character associated to an integer is obtained by chr() function. The argument for this function can be any number between 0 to 0xffff>>> chr(0xaa) 'ª' >>> chr(0xff) 'ÿ' >>> chr(200) 'È' >>> chr(122) 'z'

How to convert an integer to a unicode character in Python?

Pythonista
Updated on 09-Sep-2023 15:26:00

3K+ Views

Python library's chr() function converts Unicode character associated to any integer which is between 0 an 0x10ffff.>>> chr(36) '$' >>> chr(97) 'a' >>> chr(81) 'Q'

How to convert a single character to its integer value in Python?

Pythonista
Updated on 25-Feb-2020 11:14:47

509 Views

Each character is associated with an ASCII value which is a unique number. It is obtained by ord() function.>>> ord('A') 65 >>> ord('+') 43 >>> ord(' ')

How we can create a dictionary from a given tuple in Python?

Pythonista
Updated on 25-Feb-2020 11:14:12

315 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a','b','c','d') >>> T2=(1,2,3,4) >>> dict((x,y) for x,y in zip(t1,t2))Dictionary comprehension syntax can also be used to construct dictionary object from two tuples>>> d={k:v for (k,v) in zip(T1,T2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Advertisements