Python Container Operations
Python Container Operations
# Sample list
my_list = [3, 1, 5, 2, 4]
# remove(item): Removes the first occurrence of a specific element from the list
my_list.remove(5) # [3, 1, 9, 2, 4, 6, 7, 8]
# pop(index=-1): Removes and returns the element at the specified index, or the last
element if no index is provided
popped_element = my_list.pop(3) # [3, 1, 9, 4, 6, 7, 8], popped_element = 2
# sort(key=None, reverse=False): Sorts the list in place, optionally using a custom key
function and specifying the sort order
my_list.sort() # [1, 3, 4, 6, 7, 8, 9]
# all(iterable): Returns True if all elements in the list or an iterable are True,
otherwise False
are_all_true = all([True, True, False]) # False
# any(iterable): Returns True if at least one element in the list or an iterable is True,
otherwise False
is_any_true = any([False, False, True]) # True
# sorted(iterable, key=None, reverse=False): Returns a new sorted list from the elements of
the list or an iterable, with optional key and reverse parameters
sorted_list = sorted([3, 1, 5, 2, 4]) # [1, 2, 3, 4, 5]
# enumerate(iterable, start=0): Returns an iterator that generates pairs of index and value
for each element in the list or an iterable
for index, value in enumerate(['a', 'b', 'c'], start=1):
print(index, value)
# Output: 1 a, 2 b, 3 c
# filter(function, iterable): Returns an iterator that filters elements from the list or an
iterable based on the provided function
filtered_list = list(filter(lambda x: x > 2, [1, 2, 3, 4, 5])) # [3, 4, 5]
# map(function, iterable): Applies the provided function to each element of the list or an
iterable and returns an iterator with the results
mapped_list = list(map(lambda x: x * 2, [1, 2, 3, 4])) # [2, 4, 6, 8]
# reversed(sequence): Returns an iterator that iterates over the elements of the list or a
sequence in reverse order
reversed_list = list(reversed([1, 2, 3, 4])) # [4, 3, 2, 1]
# zip(*iterables): Returns an iterator that combines elements from multiple iterables into
tuples
zipped_list = list(zip([1, 2, 3], ['a', 'b', 'c'])) # [(1, 'a'), (2, 'b'), (3, 'c')]
# slice(start, stop, step): Returns a slice object that represents a portion of the list
sliced_list = [1, 2, 3, 4, 5]
my_slice = sliced_list[1:4:2] # [2, 4]
# fromkeys(iterable, value=None): Creates a new dictionary with keys from an iterable and
values set to a specified value
new_dict = dict.fromkeys(["name", "age", "city"], None) # {'name': None, 'age': None,
'city': None}
# get(key, default=None): Returns the value associated with a specified key, or a default
value if the key is not found
value = my_dict.get("name", "Unknown") # 'John'
# items(): Returns a view object that contains key-value pairs of the dictionary
items = my_dict.items() # [('name', 'John'), ('age', 30), ('city', 'New York')]
# keys(): Returns a view object that contains the keys of the dictionary
keys = my_dict.keys() # ['name', 'age', 'city']
# values(): Returns a view object that contains the values of the dictionary
values = my_dict.values() # ['John', 30, 'New York']
# pop(key, default=None): Removes and returns the value associated with a specified key, or
a default value if the key is not found
removed_value = my_dict.pop("age") # 30
# popitem(): Removes and returns the last inserted key-value pair as a tuple
removed_item = my_dict.popitem() # ('city', 'New York')
# update(iterable, **kwargs): Updates the dictionary with key-value pairs from an iterable
or keyword arguments
my_dict.update({"age": 35, "city": "London"}) # {'name': 'John', 'age': 35, 'city':
'London'}
# Remove elements from the set that are also present in another set.
s1.difference_update(s2)
# Now s1 is updated to: {1}
# Update the set with the intersection of itself and another set.
s1.intersection_update(s2)
# Now s1 is updated to: {2, 3}
# Remove an element from the set. Raises a KeyError if the element is not found.
s = {1, 2, 3}
s.remove(2)
# Output: {1, 3}
# Update the set with the symmetric difference of itself and another set.
s1.symmetric_difference_update(s2)
# Now s1 is updated to: {1, 4}
# Update the set with the union of itself and another set.
s1.update(s2)
# Now s1 is updated to: {1, 2, 3, 4, 5}