Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Cheat Sheet: Python Data Structures Part-2

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

1. Value = dict_name["key_name"]
You can
access the Copied!
values in a
Accessing Example:
dictionary
Values
using their
1. 1
corresponding 2. 2
keys.
1. name = person["name"]
2. age = person["age"]

Copied!

Syntax:
1. 1
Inserts a new
key-value 1. dict_name[key] = value
pair into the
dictionary. If Copied!
the key
Add or
already exists, Example:
modify
the value will
1. 1
be updated; 2. 2
otherwise, a
new entry is 1. person["Country"] = "USA" # A new entry will be created.
2. person["city"] = "Chicago" # Update the existing value for the same key
created.
Copied!
Syntax:
1. 1
Removes the
specified key- 1. del dict_name[key]
value pair
Copied!
from the
del dictionary.
Raises a Example:
KeyError if 1. 1
the key does
not exist. 1. del person["Country"]

Copied!

Syntax:
The update() 1. 1
method
merges the 1. dict_name.update({key: value})
provided
Copied!
dictionary
update() into the
existing Example:
dictionary, 1. 1
adding or
updating key- 1. person.update({"Profession": "Doctor"})
value pairs.
Copied!

The clear() Syntax:


method
empties the 1. 1
dictionary, 1. dict_name.clear()
removing all
key-value Copied!
pairs within
clear()
it. After this Example:
operation, the
dictionary is 1. 1
still 1. grades.clear()
accessible
and can be Copied!
used further.
Example:
You can
check for the 1. 1
existence of a 2. 2
key
key in a
existence 1. if "name" in person:
dictionary 2. print("Name exists in the dictionary.")
using the in
keyword Copied!

copy() Creates a Syntax:


shallow copy
of the
dictionary.
1. 1
The new
1. new_dict = dict_name.copy()
dictionary
contains the Copied!
same key-
value pairs as Example:
the original,
but they 1. 1
remain 2. 2
distinct 1. new_person = person.copy()
objects in 2. new_person = dict(person) # another way to create a copy of dictionary
memory.
Copied!

Syntax:

Retrieves all 1. 1
keys from the 1. keys_list = list(dict_name.keys())
dictionary
and converts Copied!
them into a
keys()
list. Useful Example:
for iterating
or processing 1. 1
keys using 1. person_keys = list(person.keys())
list methods.
Copied!

Syntax:

Extracts all 1. 1
values from 1. values_list = list(dict_name.values())
the dictionary
and converts Copied!
them into a
values()
list. This list Example:
can be used
for further 1. 1
processing or 1. person_values = list(person.values())
analysis.
Copied!

items() Retrieves all Syntax:


key-value
pairs as tuples 1. 1
and converts 1. items_list = list(dict_name.items())
them into a
list of tuples. Copied!
Each tuple
consists of a Example:
key and its
corresponding 1. 1
value. 1. info = list(person.items())
Copied!

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

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

1. fruits.add("mango")

Copied!

Syntax:
1. 1

1. set_name.clear()

The `clear()` method removes all Copied!


elements from the set, resulting
clear()
in an empty set. It updates the set Example:
in-place.
1. 1

1. fruits.clear()</td>

Copied!

Syntax:
1. 1

1. new_set = set_name.copy()

The `copy()` method creates a Copied!


shallow copy of the set. Any
copy()
modifications to the copy won't Example:
affect the original set.
1. 1

1. new_fruits = fruits.copy()

Copied!

Defining A set is an unordered collection Example:


Sets of unique elements. Sets are
enclosed in curly braces `{}`.
They are useful for storing
distinct values and performing
set operations.
1. 1
2. 2

1. empty_set = set() #Creating an Empty


2. Set fruits = {"apple", "banana", "orange"}

Copied!

Syntax:
1. 1

1. set_name.discard(element)

Use the `discard()` method to Copied!


remove a specific element from
discard()
the set. Ignores if the element is Example:
not found.
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 Copied!
another set. It returns True if all
issubset()
elements of the current set are Example:
present in the other set,
otherwise False. 1. 1

1. is_subset = fruits.issubset(colors)

Copied!

Syntax:

The `issuperset()` method checks is_superset = set1.issuperset(set2)


if the current set is a superset of
Example:
another set. It returns True if all
issuperset()
elements of the other set are 1. 1
present in the current set,
otherwise False. 1. is_superset = colors.issuperset(fruits)

Copied!

pop() The `pop()` method removes and Syntax:


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

Example:
1. 1

1. removed_fruit = fruits.pop()

Copied!

Syntax:
1. 1

1. set_name.remove(element)

Use the `remove()` method to Copied!


remove a specific element from
remove()
the set. Raises a `KeyError` if Example:
the element is not found.
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)

Perform various operations on Copied!


Set sets: `union`, `intersection`,
Operations `difference`, `symmetric Example:
difference`.
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!

update() The `update()` method adds Syntax:


elements from another iterable
1. 1
into the set. It maintains the
uniqueness of elements. 1. set_name.update(iterable)

Copied!

Example:

1. 1

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

© IBM Corporation. All rights reserved.

You might also like