Python Data Structure
Python Data Structure
Sets Dictionaries
Unordered collection of unique elements Key-value pairs for data mapping
by Anudesh Saini
List
A list is a versatile collection of items, ordered and changeable. It allows duplicate elements, making it ideal for storing multiple similar
values. Lists are enclosed in square brackets [ ], and elements are separated by commas.
Key Points
A list can store heterogeneous data types and provides methods like append() and remove() for modification.
A list differs from a tuple as it allows modifications, whereas a set has unique elements, and a dictionary stores key-value pairs.
List Creation
You can create a list in Python by enclosing elements in square brackets and separating them with commas. Here are some examples:
Tuples can be concatenated using the+ operator. For example, # Creation of Tuple
new_tuple = my_tuple + (4, 5) concatenates my_tuple with my_tuple = (1, 2, 3)
another tuple (4, 5).
Repetition # Indexing
first_element = my_tuple[0]
*
Tuples can be repeated using the operator. For example,
repeated_tuple = my_tuple * 3 repeats my_tuple three times. # Concatenation
new_tuple = my_tuple + (4, 5)
Slicing
:
Tuples can be sliced using the colon ( ) operator. For example, # Repetition
my_tuple[1:4] returns a new tuple containing elements from repeated_tuple = my_tuple * 3
index 1 to index 3.
# Slicing
Count sliced_tuple = my_tuple[1:4]
The count() method can be used to count the number of # Count
occurrences of a value in a tuple. For example,
count = my_tuple.count(2)
my_tuple.count(2) returns the count of occurrences of the
value 2 in the tuple.
Set
A set is an unordered collection of unique items. It is defined using curly braces { } or the set() constructor. Sets are often used for fast
membership testing and elimination of duplicate values.
Definition & Characteristics A set enforces uniqueness, ensuring each element appears
only once, and does not allow indexing.
Usage & Examples Sets are useful for finding common or distinct elements
between multiple sets using set operations like union(),
intersection(), and difference().
Python Syntax
Here are the key operations and syntax related to sets in Python:
Creation of Set: Sets can be created using curly braces { } or the set() constructor.
Adding Elements: Elements can be added to a set using the add() method or the |= operator.
Removal of Elements: Elements can be removed from a set using the remove() method or the -= operator.
Discard Element: The discard() method removes an element from a set if it is present, without raising an error if the element is not
found.
Intersection: The intersection() method or the & operator can be used to find the common elements between two sets.
Union: The union() method or the | operator can be used to find the union of two sets, i.e., all the unique elements from both sets.
Difference: The difference() method or the - operator can be used to find the elements that are present in one set but not in the
other.
Clear Set: The clear() method removes all elements from a set, making it an empty set.
Length of Set: The len() function can be used to get the number of elements in a set.
Dictionary
A dictionary is a collection of key-value pairs. It is enclosed in curly brackets { }, with each pair separated by a colon. Dictionaries are
mutable, unordered, and commonly used for data organization and efficient element retrieval.
Dictionaries are like glorified address books, where each entry (the key) is associated with specific information (the value).
Python Syntax
Here are the key operations and syntax related to dictionaries in Python:
Creating a Dictionary: Dictionaries can be created using curly brackets { } or the dict() constructor.
Modifying Values: Values in a dictionary can be modified by assigning a new value to the corresponding key.
Checking Key Existence: The `in` keyword can be used to check if a key exists in a dictionary.
Getting a Value: The `get()` method can be used to retrieve a value from a dictionary based on a key.
Length of Dictionary: The `len()` function can be used to get the number of key-value pairs in a dictionary.
Updating a Dictionary: The `update()` method can be used to add or update key-value pairs in a dictionary.
Sorting a Dictionary: Dictionaries are inherently unordered, but you can use the `sorted()` function to sort the dictionary based on
keys or values.
1. Q: What is a list in Python? A: A list is a collection of ordered and mutable elements enclosed in square brackets, e.g., [1, 2, 3].
2. Q: How do you access elements in a list? A: You can access elements in a list using index notation, e.g., my_list[0] accesses the
first element.
3. Q: How do you add an element to a list? A: You can use the append() method to add an element to the end of a list.
4. Q: What is the difference between append() and extend() methods? A: append() adds its argument as a single element, while
extend() adds each element of its argument to the list.
5. Q: How do you remove an element from a list by value? A: You can use the remove() method to remove an element by its value.
Tuple:
1. Q: What is a tuple in Python? A: A tuple is an ordered, immutable collection of elements enclosed in parentheses, e.g., (1, 2, 3).
2. Q: How do you access elements in a tuple? A: You can access elements in a tuple using index notation, just like with lists.
3. Q: Can a tuple contain mutable elements? A: Yes, a tuple can contain mutable elements like lists, but the tuple itself is still
immutable.
4. Q: How do you swap the values of two variables using tuples? A: You can use tuple unpacking, e.g., (a, b) = (b, a).
Dictionary:
1. Q: What is a dictionary in Python? A: A dictionary is an unordered collection of key-value pairs enclosed in curly braces, e.g.,
{"name": "John", "age": 30}.
2. Q: How do you access values in a dictionary? A: You can access values by their keys, e.g., my_dict["name"] returns "John".
3. Q: How do you check if a key exists in a dictionary? A: You can use the in keyword or the get() method to check if a key exists.
4. Q: How can you add a new key-value pair to a dictionary? A: You can assign a value to a new key, e.g., my_dict["city"] = "New
York".
5. Q: How can you remove a key-value pair from a dictionary? A: You can use the pop() method to remove a key-value pair, or the del
statement.
Set:
1. Q: What is a set in Python? A: A set is an unordered collection of unique elements enclosed in curly braces, e.g., {1, 2, 3}.
2. Q: How do you add elements to a set? A: You can use the add() method to add a single element or the update() method to add
multiple elements.
3. Q: How do you remove elements from a set? A: You can use the remove() method or the discard() method to remove elements by
value.
4. Q: What is the difference between a set and a frozenset? A: A set is mutable, while a frozenset is immutable and cannot be
changed after creation.
5. Q: How do you find the union of two sets? A: You can use the union() method or the | operator to find the union of two sets.
6. Q: How do you find the intersection of two sets? A: You can use the intersection() method or the & operator to find the intersection
of two sets.