0% found this document useful (0 votes)
52 views19 pages

Sets and Dictionaries

This document provides an overview of Python sets and dictionaries. It discusses that sets are unordered collections with no duplicate entries, while dictionaries store data as key-value pairs. The document then provides examples of creating, accessing, modifying, deleting items from sets and dictionaries. It also discusses built-in functions and methods for dictionaries, and when to use lists, sets, tuples or dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views19 pages

Sets and Dictionaries

This document provides an overview of Python sets and dictionaries. It discusses that sets are unordered collections with no duplicate entries, while dictionaries store data as key-value pairs. The document then provides examples of creating, accessing, modifying, deleting items from sets and dictionaries. It also discusses built-in functions and methods for dictionaries, and when to use lists, sets, tuples or dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

RAGHU EDUCATIONAL INSTITUTIONS

Python Sets &


Dictionaries
Sets

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,

ample: To create a set, you can write,


Set Operations
Set Operations
Set Operations
Set Operations
Dictionaries

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.

You might also like