Unit 4 - Lists And Sequences

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/32

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.

33 Terms

1
New cards

Lists

Ordered, mutable (changeable) collections of items

2
New cards

Tuples

Ordered, immutable (unchangeable) collections of items

3
New cards

Why Use Lists?

Versatile for storing and manipulating data that might change, like user inpts, evolving datasets, or temporary storage

4
New cards

Why Use Tuples?

Useful for representing fixed collections of data (like coordinates), returning multiple values from functions, or ensuring data integrity where changes are not desired

5
New cards

Differences In Mutability (Lists & Tuples)

Lists can be modified after creation; tuples cannot

6
New cards

Syntax Difference (Lists & Tuples)

Lists use square brackets

7
New cards

Creating Lists

Empty list: my_list = []

With elements: my_list = [1, “hell0”, 3.14]

8
New cards

Creating Tuples

Empty tuple: my_tuple = ()

With elements: my_tuple = (1, “hello”, 3.14)

9
New cards

Accessing Elements (Indexing)

Uses square brackets [] with an index

10
New cards

0-based Indexing

The first element is at index 0

11
New cards

Slicing (Extracting Sub-sequence) Syntax

sequence[start:end:step]

12
New cards

Slicing Start

Inclusive index (defaults to 0)

13
New cards

Slicing End

Exclusive index (defaults to the end of the sequence)

14
New cards

Slicing Step

The increment (defaults to 1)

15
New cards

len(my_list) or len(my_tuple)

Returns the number of elements

16
New cards

Concatenation (Joining)

Uses the + operator

17
New cards

Important About Concatenation

Creates a new list/tuple - does not modify the originals

18
New cards

Repetition

Uses the * operator (with an integer)

19
New cards

Important About Repetition

Creates a new list/tuple

20
New cards

Repetition Code

repeated_list = my_list * 3

21
New cards

my_list.append(item)

Adds item to the end of the list - modifies the list in-place

22
New cards

my_list.extend(another_list)

Appends all elements from another_list to the end of my_list - modifies my_list in-place

23
New cards

Constrast Between Extending And “+”

+ creates a new list; extend modifies the existing one

24
New cards

my_list.insert(index, item)

Inserts item at the specified index - modifies the list in_place

25
New cards

my_list.pop()

Removes and returns the last element - modifies the list in-place

26
New cards

my_list.pop(index)

Removes and returns the element at the specified index - modifies the list in-place

27
New cards

my_list.remove(value)

Removes the first occurrence of value - modifies the list in-place

28
New cards

Caution Of Removing By Value

Raises a ValueError if the value is not found

29
New cards

del my_list[index]

Removes the element at index - modifies the list in-place

30
New cards

del my_list[start:end]

Removes a slice

31
New cards

my_list[index] = new_value

Changes the element at index

32
New cards

my_list[start:end] = another_list

Replaces a slice with elements from another list (sizes don’t have to match)

33
New cards

for item in sequence: Loop

Iterates directly over elements - simple and readable