0% found this document useful (0 votes)
31 views4 pages

Cheat Sheet Python Data Structure

The document provides information about Python dictionaries and sets. It describes methods for creating, accessing, modifying, and retrieving information from dictionaries. These include adding or updating keys and values, deleting keys, copying dictionaries, and getting keys, values, and items. The document also briefly introduces sets.

Uploaded by

Aashish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
31 views4 pages

Cheat Sheet Python Data Structure

The document provides information about Python dictionaries and sets. It describes methods for creating, accessing, modifying, and retrieving information from dictionaries. These include adding or updating keys and values, deleting keys, copying dictionaries, and getting keys, values, and items. The document also briefly introduces sets.

Uploaded by

Aashish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

06/03/2024, 15:20 about:blank

Cheat Sheet: Python Data Structures Part-2

Dictionaries
Package/Method Description Code Example
Example:
1. 1
A dictionary is a built-in data type that represents 2. 2
Creating a
a collection of key-value pairs. Dictionaries are
Dictionary 1. dict_name = {} #Creates an empty dictionary
enclosed in curly braces {}.
2. person = { "name": "John", "age": 30, "city": "New York"}

Copied!
Syntax:

1. 1

1. Value = dict_name["key_name"]

Copied!
You can access the values in a dictionary using
Accessing Values Example:
their corresponding keys.
1. 1
2. 2

1. name = person["name"]
2. age = person["age"]

Copied!
Syntax:
1. 1

1. dict_name[key] = value

Copied!
Inserts a new key-value pair into the dictionary. If
Add or modify the key already exists, the value will be updated; Example:
otherwise, a new entry is created.
1. 1
2. 2

1. person["Country"] = "USA" # A new entry will be created.


2. person["city"] = "Chicago" # Update the existing value for the same key

Copied!
Syntax:
1. 1

1. del dict_name[key]

Removes the specified key-value pair from the Copied!


del dictionary. Raises a KeyError if the key does not
exist. Example:

1. 1

1. del person["Country"]

Copied!
Syntax:

1. 1

1. dict_name.update({key: value})

The update() method merges the provided Copied!


update() dictionary into the existing dictionary, adding or
updating key-value pairs. Example:

1. 1

1. person.update({"Profession": "Doctor"})

Copied!
clear() The clear() method empties the dictionary, Syntax:
removing all key-value pairs within it. After this
operation, the dictionary is still accessible and can 1. 1
be used further. 1. dict_name.clear()

Copied!

Example:
1. 1

1. grades.clear()

about:blank 1/4
06/03/2024, 15:20 about:blank
Copied!
Example:

1. 1
You can check for the existence of a key in a 2. 2
key existence
dictionary using the in keyword 1. if "name" in person:
2. print("Name exists in the dictionary.")

Copied!
Syntax:

1. 1

1. new_dict = dict_name.copy()

Creates a shallow copy of the dictionary. The new Copied!


dictionary contains the same key-value pairs as
copy() Example:
the original, but they remain distinct objects in
memory. 1. 1
2. 2

1. new_person = person.copy()
2. new_person = dict(person) # another way to create a copy of dictionary

Copied!
Syntax:

1. 1

1. keys_list = list(dict_name.keys())

Retrieves all keys from the dictionary and Copied!


keys() converts them into a list. Useful for iterating or
processing keys using list methods. Example:
1. 1

1. person_keys = list(person.keys())

Copied!
Syntax:
1. 1

1. values_list = list(dict_name.values())

Extracts all values from the dictionary and Copied!


values() converts them into a list. This list can be used for
further processing or analysis. Example:
1. 1

1. person_values = list(person.values())

Copied!
Syntax:
1. 1

1. items_list = list(dict_name.items())

Retrieves all key-value pairs as tuples and Copied!


items() converts them into a list of tuples. Each tuple
consists of a key and its corresponding value. Example:

1. 1

1. info = list(person.items())

Copied!

Sets
Package/Method Description Code Example
Syntax:
1. 1

1. set_name.add(element)

Copied!
Elements can be added to a set using the `add()` method. Duplicates are
add()
automatically removed, as sets only store unique values. Example:
1. 1

1. fruits.add("mango")

Copied!
clear() The `clear()` method removes all elements from the set, resulting in an empty set. Syntax:
It updates the set in-place.
1. 1

1. set_name.clear()

about:blank 2/4
06/03/2024, 15:20 about:blank
Copied!

Example:

1. 1

1. fruits.clear()</td>

Copied!

Syntax:
1. 1

1. new_set = set_name.copy()

Copied!
The `copy()` method creates a shallow copy of the set. Any modifications to the
copy()
copy won't affect the original set. Example:

1. 1

1. new_fruits = fruits.copy()

Copied!
Example:
1. 1
A set is an unordered collection of unique elements. Sets are enclosed in curly 2. 2
Defining Sets braces `{}`. They are useful for storing distinct values and performing set
operations. 1. empty_set = set() #Creating an Empty
2. Set fruits = {"apple", "banana", "orange"}

Copied!
Syntax:
1. 1

1. set_name.discard(element)

Copied!
Use the `discard()` method to remove a specific element from the set. Ignores if
discard()
the element is not found. Example:
1. 1

1. fruits.discard("apple")

Copied!
Syntax:
1. 1

1. is_subset = set1.issubset(set2)

The `issubset()` method checks if the current set is a subset of another set. It Copied!
issubset() returns True if all elements of the current set are present in the other set,
otherwise False. Example:
1. 1

1. is_subset = fruits.issubset(colors)

Copied!
Syntax:

is_superset = set1.issuperset(set2)
The `issuperset()` method checks if the current set is a superset of another set. It Example:
issuperset() returns True if all elements of the other set are present in the current set,
otherwise False. 1. 1

1. is_superset = colors.issuperset(fruits)

Copied!
Syntax:
1. 1

1. removed_element = set_name.pop()

The `pop()` method removes and returns an arbitrary element from the set. It Copied!
pop() raises a `KeyError` if the set is empty. Use this method to remove elements when
the order doesn't matter. Example:
1. 1

1. removed_fruit = fruits.pop()

Copied!
remove() Use the `remove()` method to remove a specific element from the set. Raises a Syntax:
`KeyError` if the element is not found.
1. 1

1. set_name.remove(element)

Copied!

about:blank 3/4
06/03/2024, 15:20 about:blank
Example:
1. 1

1. fruits.remove("banana")

Copied!
Syntax:
1. 1
2. 2
3. 3
4. 4

1. union_set = set1.union(set2)
2. intersection_set = set1.intersection(set2)
3. difference_set = set1.difference(set2)
4. sym_diff_set = set1.symmetric_difference(set2)

Copied!
Perform various operations on sets: `union`, `intersection`, `difference`,
Set Operations
`symmetric difference`. Example:
1. 1
2. 2
3. 3
4. 4

1. combined = fruits.union(colors)
2. common = fruits.intersection(colors)
3. unique_to_fruits = fruits.difference(colors)
4. sym_diff = fruits.symmetric_difference(colors)

Copied!
Syntax:
1. 1

1. set_name.update(iterable)

Copied!
The `update()` method adds elements from another iterable into the set. It
update()
maintains the uniqueness of elements. Example:
1. 1

1. fruits.update(["kiwi", "grape"])

Copied!

© IBM Corporation. All rights reserved.

about:blank 4/4

You might also like