Computer Science, asked by mithun324, 1 year ago

How are python dictionaries different from python lists?

Answers

Answered by prashanth1551
1
Python dictionaries are (apparently) random sequences that contain a set of keys, where each key points to a value and is associated with that value, hence the expression "associative arrays" sometimes found in other languages. Dictionaries act just like real life dictionaries, where the keyis the word and the value is the word's definition.

An English dictionary, for example, may contain hundreds of thousands of words. Because each word is put into the dictionary in a well defined way, the word and its definition can be found very quickly. In any real life dictionary words can be added or deleted at any time and the definition of an existing word can be changed. Although an English dictionary may appear to contain several definitions of the same word, "house (noun)" and "house (verb)" are separate entries. Python's dictionaries are conceptually similar, but the ordering of the keys may seem random. However, entries are put into Python's dictionaries in a well defined way, so that each key and its associatedvalue may be retrieved very quickly.

Dictionaries use the curly braces ({}) like the set, however empty curly braces create a dictionary.

>>> {"blue": 0, "red": 1, "green": 2} # {'green': 2, 'red': 1, 'blue': 0} # Ordering is different from that supplied above. # "blue" is a key. Its associated value is 0. # "red" is a key. Its associated value is 1. # "green" is a key. Its associated value is 2. >>> >>> {"blue": 0, "red": 1, "green": 2} == {"green": 2, "blue": 0, "red": 1} == {"green": 2, "red": 1, "blue": 0} True # Ordering is not important. >>> >>> isinstance( {"green": 2, "blue": 0, "red": 1}, dict ) True >>> >>> {30: "0", 31: "1", "32": 2} {'32': 2, 30: '0', 31: '1'} >>> >>> isinstance({}, dict) True >>> isinstance({}, set) False

Answered by zaidghori1997
0

list is a collection of elements enclosed in [ ] and dictionaries are enclosed in { }. The basic difference is in list indexing start from 0 and in dictionaries you (user) define your own index!

Similar questions