Tuples and Lists Flashcards

(31 cards)

1
Q

tuples are _____

A

immutable

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

Creating a tuple in python

A

> > > marx_tuple = (‘Groucho’, ‘Chico’, ‘Harpo’)

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

tuple() function

A

> > > marx_list = [‘Groucho’, ‘Chico’, ‘Harpo’]
tuple(marx_list)
(‘Groucho’, ‘Chico’, ‘Harpo’)

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

You can combine tuples using ___

A

+
»> (‘Groucho’,) + (‘Chico’, ‘Harpo’)
(‘Groucho’, ‘Chico’, ‘Harpo’)

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

Duplicating itesm in utple using *

A

> > > (‘yada’,) * 3
(‘yada’, ‘yada’, ‘yada’)

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

Can you directly modify a tuple?

A

No

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

You can ______ tupes to make a new one as you can with strings

A

concatenate

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

Lists in python are _____

A

mutable

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

You can make an empty list using the _____ function

A

list()

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

Making an empty list

A

empty_list = [ ]

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

you can use ____ function to chop a string into a list by some separator

A

split()
»> talk_like_a_pirate_day = ‘9/19/2019’
»> talk_like_a_pirate_day.split(‘/’)
[‘9’, ‘19’, ‘2019’]

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

> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
Hwo to get first item

A

marxes[0]

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

How to get first two elements

A

> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
marxes[0:2]
[‘Groucho’, ‘Chico’]

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

How to reverse a list

A

.reverse()

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

How to appendx ‘Zeppo’ to end of this list?
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]

A

> > > marxes.append(‘Zeppo’)
marxes
[‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]

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

How to insert a string at a given index in this list
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]

A

> > > marxes.insert(2, ‘Gummo’)

17
Q

Duplicate items with __

18
Q

Combine lists using ____ or ___

A

extend(), +, +=

19
Q

How to change 3rd item from Harpo to Wanda
»> marxes.insert(2, ‘Gummo’)

A

> > > marxes[2] = ‘Wanda’

20
Q

How to change 2nd and third items to 8 and 9
»> numbers = [1, 2, 3, 4]

A

> > > numbers[1:3] = [8, 9]

21
Q

How to deleet Karl from list
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Gummo’, ‘Karl’]

A

> > > del marxes[-1]

22
Q

You can also delete at item using the _____ function

23
Q

> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]
How to pop of the last element?

A

> > > marxes.pop()
‘Zeppo’
marxes
[‘Groucho’, ‘Chico’, ‘Harpo’]

24
Q

How to find index of Chico
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]

A

> > > marxes.index(‘Chico’)
1

25
How to chekc if Groucho is in marxes >>> marxes = ['Groucho', 'Chico', 'Harpo', 'Zeppo']
>>> 'Groucho' in marxes True >>> 'Bob' in marxes False
26
Count occurrences of value with ____
count
27
*The list method _____ sorts the list itself, in place.
sort()
28
*The general function ____ returns a sorted copy of the list.
sorted()
29
You can copy the values of a list to an independent, fresh list by using any of these methods:
- The list copy() method *The list() conversion function *The list slice [:]
30
How to iterate over a list
>>> cheeses = ['brie', 'gjetost', 'havarti'] >>> for cheese in cheeses: ... print(cheese)
31
Lists can contain elements of different types, including other lists, as illustrated here:
>>> small_birds = ['hummingbird', 'finch'] >>> extinct_birds = ['dodo', 'passenger pigeon', 'Norwegian Blue'] >>> carol_birds = [3, 'French hens', 2, 'turtledoves'] >>> all_birds = [small_birds, extinct_birds, 'macaw', carol_birds]