Tuples and Lists Flashcards

(58 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’)

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

Duplicate items with __

A

*

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

Combine lists using ____ or ___

A

extend(), +, +=

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

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

A

> > > marxes[2] = ‘Wanda’

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

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

A

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

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

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

A

> > > del marxes[-1]

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

You can also delete at item using the _____ function

A

remove()

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

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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]
32
Tuple items are indexed, the first item has index _____, the second item has index ____ etc.
[0],[1]
33
Since tuples are indexed, they can have items with the _____ value
same
34
To determine length of tuple
len()
35
Creat a tuplewith only one item
thistuple = ("apple",) print(type(thistuple))
36
Tuple items can be of any data type
true
37
mytuple = ("apple", "banana", "cherry") Print the type
print(type(mytuple))
38
It is also possible to use the ______ constructor to make a tuple.
tuple()
39
You can access tuple items by referring to the index number, inside ____ brackets
square print(thistuple[1])
40
You can also use negative indexing
True
41
Slicing example for tuple
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[2:5])
42
How to access list item
listname[index]
43
To change the value of a specific item, refer to the _____ ______
index number thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thislist)
44
How to change range of items in a list
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"] thislist[1:3] = ["blackcurrant", "watermelon"] print(thislist)
45
How to add items to end of list
append()
46
How to insert items at specific index in list
insert()
47
How to append items from another list
extend()
48
The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.)
True
49
How to remove item from list
remove()
50
The _____ method removes the specified index
pop() ex: thisList.pop(1)
51
If you do not specify the index, the pop() method removes the ______ item
last
52
The ____ keyword also removes the specified index
del thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
53
The ______ method empties the list.
clear()
54
List objects have a _____ method that will sort the list alphanumerically, ascending, by default:
sort()
55
You can also customize your own function by using the keyword argument __________
key = function def myfunc(n): return abs(n - 50) thislist = [100, 50, 65, 82, 23] thislist.sort(key = myfunc) print(thislist)
56
How to copy list
copy()
57
How to make a list
use built in method list()
58
You can also make a copy of a list by using the ____ (____) operator.
:, slice