Skip to content

Latest commit

 

History

History
317 lines (211 loc) · 5.26 KB

File metadata and controls

317 lines (211 loc) · 5.26 KB

Lists

A tuple is an ordered, mutable collection of items.

my_list = [1, 2, 3, 4, 5]

print(my_list[0])  # 1
print(my_list[-1])  # 5

Slicing

print(my_list[1:3])  # [2, 3]
print(my_list[:2])  # [1, 2]
print(my_list[2:])  # [3, 4, 5]

Adding Elements

  • append(): Adds an element to the end.

    my_list.append(6)
  • insert(): Adds an element at a specific position.

    my_list.insert(2, 'a')
  • extend(): Adds multiple elements.

    my_list.extend([7, 8])

Removing Elements

  • remove(): Removes the first occurrence of an element.

    my_list.remove(2)
  • pop(): Removes and returns the element at the given index.

    my_list.pop(1)
  • clear(): Removes all elements.

    my_list.clear()

List Comprehensions

squares = [x**2 for x in range(10)]

List Techniques

  • Sorting: Use sort() for in-place sorting or sorted() to return a new sorted list. Reverse is set to True to make the sorting descending.

    my_list.sort()
    sorted_list = sorted(my_list, reverse=True)
  • Filtering: Using list comprehensions.

    even_numbers = [x for x in my_list if x % 2 == 0]
  • Mapping: Applying a function to all elements.

    squared = list(map(lambda x: x**2, my_list))
    
    # Another example
    def addition(n):
      return n + n
    result = map(addition, my_list)

Tuples

A tuple is an ordered, immutable collection of items.

my_tuple = (1, 2, 3)

print(my_tuple[0])  # 1
print(my_tuple[-1])  # 3

Tuple Unpacking

Extracting values from a tuple.

a, b, c = my_tuple

Advanced Tuple Techniques

  • Creating Single-element Tuples: Add a comma after the element.

    single_element_tuple = (1,)
  • Tuple Comprehensions: Using tuple() and generator expressions.

    squared_tuple = tuple(x**2 for x in range(10))
  • Named Tuples: Creating more readable tuples using collections.namedtuple.

    from collections import namedtuple
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(1, 2)
    print(p.x, p.y)  # 1 2

Sets

A set is an unordered, mutable collection of unique elements.

my_set = {1, 2, 3} # my_set = set({1, 2, 3})
  • add(): Adds an element.

    my_set.add(4)
  • update(): Adds multiple elements.

    my_set.update([5, 6])

Removing Elements

  • remove(): Removes an element (raises an error if not found).

    my_set.remove(2)
  • discard(): Removes an element (doesn't raise an error if not found).

    my_set.discard(2)
  • pop(): Removes and returns an arbitrary element.

    my_set.pop()
  • clear(): Removes all elements.

    my_set.clear()

Set Operations

  • Union: Combine sets.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    union_set = set1 | set2  # {1, 2, 3, 4, 5}
  • Intersection: Elements common to both sets.

    intersection_set = set1 & set2  # {3}
  • Difference: Elements in one set but not the other.

    difference_set = set1 - set2  # {1, 2}
  • Symmetric Difference: Elements in either set, but not both.

    symmetric_difference_set = set1 ^ set2  # {1, 2, 4, 5}

Advanced Set Techniques

  • Set Comprehensions: Creating sets with a comprehension.

    squared_set = {x**2 for x in range(10)}
  • Frozen Sets: Immutable sets.

    frozen_set = frozenset([1, 2, 3])

Dictionaries

A dictionary is an unordered, mutable collection of key-value pairs.

my_dict = {'name': 'Alice', 'age': 25}

Accessing Elements

Use keys to access values.

print(my_dict['name'])  # Alice

Adding/Updating Elements

  • Adding: Use a new key.

    my_dict['address'] = '123 Main St'
  • Updating: Use an existing key.

    my_dict['age'] = 26

Removing Elements

  • pop(): Removes and returns an element by key.

    age = my_dict.pop('age')
  • popitem(): Removes and returns the last key-value pair.

    last_item = my_dict.popitem()
  • del: Removes an element by key.

    del my_dict['name']
  • clear(): Removes all elements.

    my_dict.clear()

Dictionary Comprehensions

A brief way to create dictionaries.

squares = {x: x**2 for x in range(10)}

Advanced Dictionary Techniques

  • Iterating: Using .items(), .keys(), and .values().

    for key, value in my_dict.items():
        print(key, value)
  • Default Values: Using get() to avoid KeyError.

    name = my_dict.get('name', 'Unknown')
  • Merging: Combining dictionaries.

    dict1 = {'a': 1, 'b': 2}
    dict2 = {'b': 3, 'c': 4}
    merged_dict = {**dict1, **dict2}
  • Default Dictionaries: Using collections.defaultdict for automatic default values.

    from collections import defaultdict
    default_dict = defaultdict(int)
    default_dict['a'] += 1

Go Back