In Python, the following data types are supported: String, list, and sequence. They use integers as indexes. If you try to index other types, an error will occur.
[Python]
>>> List = [1, 2, 3]
>>> List [0]
1
>>> List ['one']
Traceback (most recent call last ):
File "<pyshell #34>", line 1, in <module>
List ['one']
TypeError: list indices must be integers, not str
>>> List = [1, 2, 3]
>>> List [0]
1
>>> List ['one']
Traceback (most recent call last ):
File "<pyshell #34>", line 1, in <module>
List ['one']
TypeError: list indices must be integers, not str
The dictionary index can be a string. In addition to this, it is very similar to its combination type. Of course, the dictionary index can also be an integer.
[Python]
>>> Dict1 = {'Mother ': 'Mom', 'Father ', 'Daddy '}
SyntaxError: invalid syntax
>>> Dict1 = {'Mother ': 'Mom', 'Father ': 'Daddy '}
>>> Dict1
{'Father ': 'Daddy', 'Mother ': 'Mama '}
>>> Dict1 = {'Mother ': 'Mom', 'Father ', 'Daddy '}
SyntaxError: invalid syntax
>>> Dict1 = {'Mother ': 'Mom', 'Father ': 'Daddy '}
>>> Dict1
{'Father ': 'Daddy', 'Mother ': 'mame'} we can also create an empty dictionary and add elements to it.
[Python]
>>> Eng2sp = {}
>>> Eng2sp ['one'] = 'uno'
>>> Eng2sp ['two'] = 'dos'
>>> Eng2sp
{'One': 'uno', 'two': 'dos '}
>>> Eng2sp = {}
>>> Eng2sp ['one'] = 'uno'
>>> Eng2sp ['two'] = 'dos'
>>> Eng2sp
{'One': 'uno', 'two': 'dos '} dictionary elements use commas as separators. Each element contains keys and key values, which are separated by colons.
Delete a dictionary
[Python]
>>> Inventory = {'append': 430, 'banas': 312, 'oranges': 525}
>>> Inventory
{'Banas': 312, 'append': 430, 'oranges': 525}
>>> Del inventory ['append']
>>> Inventory
{'Banas': 312, 'oranges': 525}
>>> Inventory = {'append': 430, 'banas': 312, 'oranges': 525}
>>> Inventory
{'Banas': 312, 'append': 430, 'oranges': 525}
>>> Del inventory ['append']
>>> Inventory
{'Banas': 312, 'oranges': 525}
To delete all elements, use the clear method.
[Python]
>>> Inventory. clear
<Built-in method clear of dict object at 0x0000000003289588>
>>> Inventory
{'Banas': 312, 'oranges': 525}
>>> Inventory. clear ()
>>> Inventory
{}
>>> Inventory. clear
<Built-in method clear of dict object at 0x0000000003289588>
>>> Inventory
{'Banas': 312, 'oranges': 525}
>>> Inventory. clear ()
>>> Inventory
{}
Returns the number of dictionary elements using the len function.
[Python]
>>> OS = {1: 'linux ', 2: 'windows '}
>>> Len (OS)
2
>>> OS = {1: 'linux ', 2: 'windows '}
>>> Len (OS)
2
The dictionary can be changed. If you want to modify the dictionary and keep the original backup, you need to use the dictionary copy method. Let's take a look at the example below:
[Python]
>>> Opp = {'up': 'down', 'right': 'left', 'true': 'false '}
>>> Alias = opp
>>> Alias
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (opp)
53056584
>>> Id (alias)
53056584
>>> Other = opp. copy ()
>>> Other
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (other)
52898824
>>> Opp = {'up': 'down', 'right': 'left', 'true': 'false '}
>>> Alias = opp
>>> Alias
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (opp)
53056584
>>> Id (alias)
53056584
>>> Other = opp. copy ()
>>> Other
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (other)
52898824