Posted on Leave a comment

Basic Python Data Structures – Lists, tuples, sets, dictionaries

lists tuples sets dictionaries python

List methods

  • list.append(x)
    Add an item to the end of the list. Equivalent to a[len(a):] = [x].
  • list.extend(iterable)
    Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
  • list.insert(i, x)
    Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
  • list.remove(x)
    Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
  • list.pop([i])
    Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
  • list.clear()
    Remove all items from the list. Equivalent to del a[:].
  • list.index(x[, start[, end]])
    Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
  • list.count(x)
    Return the number of times x appears in the list.
  • list.sort(*, key=None, reverse=False)
    Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
  • list.reverse()
    Reverse the elements of the list in place.
  • list.copy()
    Return a shallow copy of the list. Equivalent to a[:].

Tuple methods

  • tuple.count()
    Returns the number of times a specified value occurs in a tuple.
  • tuple.index()
    Searches the tuple for a specified value and returns the position of where it was found.

Set methods

  • set.add()
    Adds an element to the set.
  • set.clear()
    Removes all the elements from the set.
  • set.copy()
    Returns a copy of the set.
  • set.difference()
    Returns a set containing the difference between two or more sets.
  • set.difference_update()
    Removes the items in this set that are also included in another, specified set.
  • set.discard()
    Remove the specified item.
  • set.intersection()
    Returns a set, that is the intersection of two or more sets.
  • set.intersection_update()
    Removes the items in this set that are not present in other, specified set(s).
  • set.isdisjoint()
    Returns whether two sets have a intersection or not.
  • set.issubset()
    Returns whether another set contains this set or not.
  • set.issuperset()
    Returns whether this set contains another set or not.
  • set.pop()
    Removes an element from the set.
  • set.remove()
    Removes the specified element.
  • set.symmetric_difference()
    Returns a set with the symmetric differences of two sets.
  • set.symmetric_difference_update()
    Inserts the symmetric differences from this set and another.
  • set.union()
    Return a set containing the union of sets.
  • set.update()
    Update the set with another set, or any other iterable.

Dictionary methods

  • dict.clear()
    Removes all the elements from the dictionary.
  • dict.copy()
    Returns a copy of the dictionary.
  • dict.fromkeys()
    Returns a dictionary with the specified keys and value.
  • dict.get()
    Returns the value of the specified key.
  • dict.items()
    Returns a list containing a tuple for each key value pair.
  • dict.keys()
    Returns a list containing the dictionary’s keys.
  • dict.pop()
    Removes the element with the specified key.
  • dict.popitem()
    Removes the last inserted key-value pair.
  • dict.setdefault()
    Returns the value of the specified key. If the key does not exist: insert the key, with the specified value.
  • dict.update()
    Updates the dictionary with the specified key-value pairs.
  • dict.values()
    Returns a list of all the values in the dictionary.
Leave a Reply

Your email address will not be published. Required fields are marked *