CSC 203 chapter 5: Sequences: Lists and Tuples

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/48

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

49 Terms

1
New cards

Lists

mutable collection of data. Typically store homogeneous data, but may store heterogeneous data

2
New cards

Tuples

immutable collection of data. Can hold items of the same or different types

3
New cards

Accessing Elements of a List

Reference a list element by writing the list’s name followed by the element’s index enclosed in [ ] (the subscription operator)

4
New cards

Determining a List’s Length

len(listname)

5
New cards

Lists can be accessed from the end by

using negative indices

6
New cards

Immutable objects

strings and tuples

7
New cards

Appending to a List with +=

Lists can grow dynamically to accommodate new items. When the left operand of += is a list, the right operand must be an iterable- otherwise, a TypeError occurs.

8
New cards

Can concatenate two lists, two tuples, or two strings

using + to create a new sequence of the same type

9
New cards

To create an empty tuple,

use empty parentheses.

10
New cards

Pack a tuple by

separating its values with commas.

11
New cards

What is required when creating a one element tuple?

a comma (element,)

12
New cards

Adding Items to a String or Tuple

  • += can be used with strings and tuples, even though they're immutable.

  • Creates new objects.

13
New cards

Appending Tuples to Lists

listname += tuple

14
New cards

Can unpack any sequence's elements by

assigning the sequence to a comma-separated list of variables (of the 
appropriate length).

15
New cards

Swapping Values Via Packing and Unpacking

number1, number2 = (number2, number1)

16
New cards

enumerate() and then list() or tuple()

  1. adds a counter to each item in a list or any other iterable

  2. returns a list or tuple of tuples containing the index position and the element for each element of the iterable

17
New cards

slicing

Can slice sequences to create new sequences of the same type containing subsets of the original elements. [start:end+1:step]

18
New cards

[:end]

If you omit the starting index, 0 is assumed

19
New cards

[start:]

Assumes the sequence's length as the ending index.

20
New cards

Specifying a Slice with No Indices

full sequence returned

21
New cards

Slicing with Negative Indices and Steps

sequence is returned backwards

22
New cards

You can modify a list by

assigning to a slice of it—the rest of the list is unchanged

23
New cards

Deleting the Element at a Specific List Index

del list[index]

24
New cards

Deleting a Slice from a List

del list[start:end+1:step]

25
New cards

Deleting a Slice Representing the Entire List

del list[:] empties the list

26
New cards

Deleting a Variable from the Current Session

del listname deletes the list

27
New cards

When you pass a tuple to a function, attempting to modify the tuple’s immutable elements

results in a TypeError

28
New cards

.sort()

sorts a list in ascending order

29
New cards

.sort(reverse=True)

sorts a list in descending order

30
New cards

sorted()

returns a new list containing the sorted elements of its argument sequence. The original sequence is unmodified

31
New cards

Searching

the process of locating a particular key value.

32
New cards

list.index(element)

  • Searches through a list from index 0 and returns the index of the first element that matches the search key.

  • ValueError if the value is not in the list.

33
New cards

list.index(element, start)

defines the index where to begin searching for the element’s index

34
New cards

list.index(element, start, end)

defines the range of indices to search for the element’s index

35
New cards

Operator in tests whether its

right operand's iterable contains the left operand's value.

36
New cards

Operator not in tests whether its

right operand's iterable does not contain the left operand's value.

37
New cards

list.insert(index,element)

Inserting an Element at a Specific List Index

38
New cards

list.append(element)

Adding an Element to the End of a List

39
New cards

list.extend(iterable)

Adding All the Elements of a Sequence to the End of a List

40
New cards

list.remove(element)

removes the first occurrence of the element with the specified value

41
New cards

list.clear()

empties a list

42
New cards

list.count(value)

Counting the Number of Occurrences of an Item

43
New cards

list.reverse()

reverses the order of the elements

44
New cards

list.copy()

Method copy returns a new list containing a shallow copy.

45
New cards

list.pop()

By default, the last element is removed. Can also specify the index

46
New cards

List comprehension

[expression for item in iterable if condition]

  • expression: operation or value to include in the new list.

  • item: current element from the iterable.

  • iterable: sequence like a list, tuple, or range.

  • if condition (optional): filter to include only items that satisfy the condition.

47
New cards

Example of Iterating Backwards Through a Sequence

reversed_numbers = [item ** 2 for item in reversed(list)]

48
New cards

zip(iterator1, iterator2, iterator3 ...)

  • enables you to iterate over multiple iterables of data at the same time.

  • Receives any number of iterables and returns an iterator that produces tuples containing the elements at the same index in each.

49
New cards

list[rowindex][columnindex]

  • Lists can contain other lists as elements.

  • Typical use is to represent tables of values consisting of information arranged in rows and columns.

  • To identify a particular table element, we specify two indices-the first identifies the element's row, the second the element's column.