GE_8151 - Unit 4 - Lists

Course focusing on the review of Lists in Unit 4.

28 flashcards · Programming Languages · by anandvenkat4

  1. A list is an ordered collection of values

    Collection of Items.

  2. The values that make up a list are called its elements, or its items

    They are also referred to as Members of the list.

  3. A list within another list is said to be nested. From Allen Downey's glossary, A list that is an element of another list is a nested list.

  4. A list with no elements is called an empty list, and is denoted [].

    Secondary entry is also referred to as the index operator in Python.

  5. The function len() returns the length of a list, which is equal to the number of its elements.

  6. in and not in are Boolean operators that test membership in a sequence.

  7. The + operator concatenates lists.

  8. Unlike strings, lists are mutable, which means we can change their elements

    Clue: what are all the characters in the movie series in X-men?

  9. The del statement removes an element from a list.

  10. If we want to modify a list and still keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning, to avoid the ambiguity of the word copy.

  11. Passing a list as an argument actually passes a reference to the list, not a copy or clone of the list.

  12. The dot operator can also be used to access built-in methods of list objects.

  13. append(value) is a list method which adds the argument passed to it to the end of the list.

  14. The split method breaks a string into a list of words.

  15. Python has a built-in type conversion function called list that tries to turn whatever you give it into a list.

  16. What is the Python interpreter’s response to the following? list(range(10, 0, -2))

    [10, 8, 6, 4, 2]

  17. list.append(x)

    Add an item to the end of the list. Equivalent to a[len(a):] = [x].

  18. list.extend(iterable)

    Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.

  19. 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).

  20. 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.

  21. 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.)

  22. list.clear()

    Remove all items from the list. Equivalent to del a[:].

  23. 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.

  24. list.count(x)

    Return the number of times x appears in the list.

  25. 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).

  26. list.reverse()

    Reverse the elements of the list in place.

  27. list.copy()

    Return a shallow copy of the list. Equivalent to a[:].

  28. Guess the output: a = [1, 2, 3, 4, 5] a.append(6) a.append(7) a.append(7)

    a: [1, 2, 3, 4, 5, 6, 7, 7]