Chapter 7 - Lists and Tuples Flashcards

1
Q

What is a sequence

A

an object that contains multiple items of data

the items are stored in sequence, one after another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Python provides different types of sequences including lists and tuples. The difference between these:

A

a list is mutable (changeable)

a tuple is immutable (unchangeable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a list:

A

an object that contains multiple data items

can hold items of different types

the items are enclosed in brackets and separated by commas
list = [‘item1’,’item2’,’item3’]

The print function can be used to display an entire list

list() function can convert certain types of objects into lists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is an element

A

an item stored in a list

format:
list = [‘item1’,’item2’,’item3’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

repetition operator:

A

makes multiple copies of a list and joins them together.

the * symbol is a repitition operator when applied to a sequence and an integer.

sequence is left operand, number is right

format:
list*n
you can iterate over a list using a for loop

format:
for x in list:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is an index:

A

a number specifying the position of an element in a list.
enables access to individual element in list
index of first element in list is 0 and the last is -1
negative indexes identify positions relative to the end of the list.

-1 is the last element of the list, -2 is the second to last, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do white spaces also take up index positions?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how is an index exception raised

A

if an invalid index is used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does the len() function do?

A

returns the length of a sequence such as a list
example: size = len(my_list)

returns the # of elements in the list, so the index of last element is len(list)-1
can be used to prevent an index error exception when iterating over a list with a loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

LISTS ARE MUTABLE:

what is a mutable sequence

A

the items in the sequence can be changed

example:
list[1] = new_value

can be used to assign a new value to a list element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

concatenation:

A

joining two things together
+, +=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is an empty list?

A

a list that contains no elements

example:
list = []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

List slicing

slice:

A

a span of items that are taken from a sequence

format: list[start:end]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

span:

A

a list containing copies of each element. from start up to, but NOT INCLUDING, end.

if start not specified, 0 is used. if end not specified, len(list) is used for end index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does this do:

item in list

A

returns true if item is in the list.

the in operator will help to determine whether an item is contained in a list

similarly you can use not in to see if an item is not in a list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

append(item)

A

used to add items to the end of a list, by modifying the original list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

extend(item)

A

takes a list as an argument and appends all of the elements.

18
Q

sort(item)

A

used to sort the elements of the list in ascending order

t = [d,c,e,b,a]

t.sort()
t
[a,b,c,d,e]

19
Q

index(item)

A

used to determine where an item is located in a list.

returns the index of the 1st element in the list containing item.

raised value error exception if item not in list

20
Q

insert(index,item)

A

used to insert item at position index in the list

21
Q

del statement

A

removes an element from a specific index in a list

format: del list[i]

if you delete a list it is gone

22
Q

min and max functions:

A

built-in functions that returns the item that has the lowest or highest value in the sequence

sequence is passed as an argument

23
Q

remove(item)

A

removes the first occurrence of item from the list. Value error raised if item not found in list

24
Q

reverse()

A

reverses the order of the items in the list

25
Q

2 methods to copy a list:

A

1.) creating a new empty list and using a for loop to add a copy of each element from the original list to the new list

2.) creating a new empty list and concatenations the old list to the new empty list

26
Q

s.capitilize()

A

capitilzes the strings in a list ( you can use a function for this) a for loop

def capitilize_all(t):
res = []
for s in t:
res.append(s.capitilize())
return res

27
Q

isupper()

A

can be used to check and see if the string contains only upper case letters

28
Q

sum()

A

can add up all of the elements in a list. an operation like this that combines a sequence of elements into a single value is sometimes called REDUCE.

29
Q

List comprehension:

A

a concise expression that creates a new list by iterating over the elements of an existing list

list1= [1,2,3,4]
list2=[]

for item in list1:
list2.append(item)

or using list comprehension:

list1= [1,2,3,4]
list2 = [item for item in list1]

30
Q

2 dimensional list

A

a list that contains other lists as its elements.

(AKA nested lists)
common to think of these as having rows and columns

useful for working with multiple sets of data.

to process these lists need to use two indexes.

typically use nested loops to process

31
Q

tuple:

A

an immutable sequence and very similar to a list

once created cannot be changed

format:
tuple_name = (item1,Item2)

Tuples support operations as lists:
-subscript indexing for retrieving elements
-methods such as index
-built in functions: len,max,min
-slicing expressions
-the in,+,and * operators

32
Q

Tuples do not support:

A

-append
-remove
-insert
-reverse
-sort

33
Q

Advantages for using tuples over lists

A

1.) processing is faster than processing lists

2.) tuples are safer

34
Q

list() function

A

converts tuple to list

35
Q

tuple()

A

converts list to tuple

36
Q

pop(item)

A

if you know the index of an element use pop(this is rare) to delete it. if you dont provide an index it deletes and returns the last element. pop will retain the value in a variable

37
Q

remove(item)

A

removes the first occurrence of item in the list

38
Q

reverse()

A

reverses the order of the elements in the list

39
Q

del statement

A

removes an element from a specific index in a list

del list [2]

40
Q

is operator

A

used to check whether two variables refer to the same object

if:
a = “banana”
b = “banana”

a is b
true

but when you create two lists, you get two objects. even if the elements are the same, the statement would then be false

41
Q

A list can contain another list, the nested list still counts as a single element. True or false

A

true.

example:

[‘spam’,1,[‘brie’,’ryan’,’poll’],[1,2,3]]

the inside list is only one element making a total of 4 elements