🐍

Python: Data Structures & Comprehensions

125 cards · 🇺🇸 English

List/dict/set methods plus list, dict, and generator comprehensions.

Study all 125 cards for free

Flashcards, quizzes, pixel-art games and a daily review that remembers for you.

Study this deck free

Try the first 10 cards

1 / 10
Question

List.append(x)

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

Already have an account? Sign in

Cards

  1. 1List.append(x)
    Adds an item to the end of the list (similar to a[len(a):] = [x])
  2. 2List.extend(iterable)
    Extends the list by appending all items from the iterable (similar to a[len(a):] = iterable)
  3. 3List.insert(i, x)
    Inserts an item at a given position (index i). a.insert(0, x) inserts at front, a.insert(len(a), x) is equivalent to append
  4. 4List.remove(x)
    Removes the first item whose value equals x. Raises ValueError if no such item
  5. 5List.pop([i])
    Removes and returns the item at position i. Without index, removes and returns the last item. Raises IndexError if list is empty or index out of range
  6. 6List.clear()
    Removes all items from the list (similar to del a[:])
  7. 7List.index(x[, start[, end]])
    Returns zero-based index of first occurrence of x. Raises ValueError if no such item. Optional start/end limit the search
  8. 8List.count(x)
    Returns the number of times x appears in the list
  9. 9List.sort(*, key=None, reverse=False)
    Sorts the items of the list in place
  10. 10List.reverse()
    Reverses the elements of the list in place
  11. 11List.copy()
    Returns a shallow copy of the list (similar to a[:])
  12. 12List methods like insert, remove, sort
    None - they modify the list in place and return the default None (design principle for all mutable data structures in Python)
  13. 13Use a list as a stack
    Use append() to add to top, pop() without index to retrieve from top (last-in, first-out)
  14. 14Why are lists not efficient as queues?
    Appends/pops from end are fast, but inserts/pops from beginning are slow (all elements must shift by one)
  15. 15What should you use instead of lists for queues?
    collections.deque which has fast appends and pops from both ends
  16. 16List comprehension syntax
    [expression for item in iterable if condition] - creates a new list
  17. 17Equivalent of [x**2 for x in range(10)]
    list(map(lambda x: x**2, range(10))) or a for loop with append
  18. 18Must tuple expressions in list comprehensions be parenthesized?
    Yes - [(x, y) for x in range(3)] works, but [x, y for x in range(3)] raises SyntaxError
  19. 19Can list comprehensions be nested?
    Yes - the initial expression can be another list comprehension
  20. 20del a[i] vs a.pop(i)
    del removes the item; pop removes and returns it
  21. 21Del a[:]
    Clears the entire list (same as a.clear())
  22. 22Can del delete entire variables?
    Yes - del a deletes the variable a
  23. 23Are tuples mutable or immutable?
    Immutable - you cannot assign to individual items
  24. 24Can tuples contain mutable objects?
    Yes - e.g., v = ([1, 2], [3, 4]) is valid
  25. 25Create an empty tuple
    Use empty parentheses: ()
  26. 26Create a tuple with one item
    Follow the value with a comma: ('hello',) - parentheses alone are not sufficient
  27. 27Tuple packing
    t = 12345, 54321, 'hello!' - values are packed together in a tuple
  28. 28Sequence unpacking
    x, y, z = t - assigns each element of the sequence to a variable (requires same number of variables as elements)
  29. 29Set in Python
    An unordered collection with no duplicate elements
  30. 30Create an empty set
    Use set() - not {} which creates an empty dictionary

And 95 more cards. Study the whole deck free

Articles about this deck

More Programming decks

See all
Study all 125 cards free