Sets and Dictionaries
Sets and Dictionaries
Sets and Dictionaries
Sets is another data structure supported by Python. Basically, sets are same as li
with a difference that sets are lists with no duplicate entries. Technically, a set is
mutable and an unordered collection of items. This means that we can easily add
remove items from it.
A set is created by placing all the elements inside curly brackets {}, separated b
or by using the
built-in function set(). The syntax of creating a set can be given as,
Dictionary is a data structure in which we store values as a pair of key and value
key is separated from its value by a colon (:), and consecutive items are separate
commas. The entire items in a dictionary are enclosed in curly brackets({}). The
for defining a dictionary is
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}
If there are many keys and values in dictionaries, then we can also write just one
value pair on a line
to make the code easier to read and understand. This is shown below.
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, ….}
Exampl
e:
Accessing Values
Exampl
e:
Adding and Modifying an Item in a Dictiona
Exampl
e:
Modifying an Entry
Exampl
e:
Deleting Items
You can delete one or more items using the del keyword. To delete or remov
items in just one statement, use the clear() function. Finally, to remove a
dictionary from the memory, we can gain
use the del statement as del Dict_name. The syntax to use the del statemen
given as,
del dictionary_variable[key]
Exampl
e:
Sorting Items and Looping over Items in a
Dictionary
Exampl
es:
Nested Dictionaries
Exampl
e:
Built-in Dictionary Functions and Methods
Built-in Dictionary Functions and Methods
Built-in Dictionary Functions and Methods
Difference between a List and a Dictionar
First, a list is an ordered set of items. But, a dictionary is a data structure that is
matching one item
(key) with another (value).
• Second, in lists, you can use indexing to access a particular item. But, thes
should be a number. In
dictionaries, you can use any type (immutable) of value as an index. For examp
we write Dict['Name'], Name acts as an index but it is not a number but a string.
• Third, lists are used to look up a value whereas a dictionary is used to take o
and look up another
value. For this reason, dictionary is also known as a lookup table.
Fourth, the key-value pair may not be displayed in the order in which it was
while defining the
dictionary. This is because Python uses complex algorithms (called hashing) t
fast access to the items stored in the dictionary. This also makes dictionary pref
use over a list of tuples.
String Formatting with Dictionaries
Python also allows you to use string formatting feature with dictionaries. So you
%s, %d, %f, etc. to
represent string, integer, floating point number, or any other data.
Example: Program that uses string formatting feature to print the key-value pair
in the dictionary
When to use which Data Structure?
• Use lists to store a collection of data that does not need random access.
• Use lists if the data has to be modified frequently.
• Use a set if you want to ensure that every element in the data structure
unique.
• Use tuples when you want that your data should not be altered.