Tuples and Lists Flashcards
(31 cards)
tuples are _____
immutable
Creating a tuple in python
> > > marx_tuple = (‘Groucho’, ‘Chico’, ‘Harpo’)
tuple() function
> > > marx_list = [‘Groucho’, ‘Chico’, ‘Harpo’]
tuple(marx_list)
(‘Groucho’, ‘Chico’, ‘Harpo’)
You can combine tuples using ___
+
»> (‘Groucho’,) + (‘Chico’, ‘Harpo’)
(‘Groucho’, ‘Chico’, ‘Harpo’)
Duplicating itesm in utple using *
> > > (‘yada’,) * 3
(‘yada’, ‘yada’, ‘yada’)
Can you directly modify a tuple?
No
You can ______ tupes to make a new one as you can with strings
concatenate
Lists in python are _____
mutable
You can make an empty list using the _____ function
list()
Making an empty list
empty_list = [ ]
you can use ____ function to chop a string into a list by some separator
split()
»> talk_like_a_pirate_day = ‘9/19/2019’
»> talk_like_a_pirate_day.split(‘/’)
[‘9’, ‘19’, ‘2019’]
> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
Hwo to get first item
marxes[0]
How to get first two elements
> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
marxes[0:2]
[‘Groucho’, ‘Chico’]
How to reverse a list
.reverse()
How to appendx ‘Zeppo’ to end of this list?
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
> > > marxes.append(‘Zeppo’)
marxes
[‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]
How to insert a string at a given index in this list
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’]
> > > marxes.insert(2, ‘Gummo’)
Duplicate items with __
*
Combine lists using ____ or ___
extend(), +, +=
How to change 3rd item from Harpo to Wanda
»> marxes.insert(2, ‘Gummo’)
> > > marxes[2] = ‘Wanda’
How to change 2nd and third items to 8 and 9
»> numbers = [1, 2, 3, 4]
> > > numbers[1:3] = [8, 9]
How to deleet Karl from list
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Gummo’, ‘Karl’]
> > > del marxes[-1]
You can also delete at item using the _____ function
remove()
> > > marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]
How to pop of the last element?
> > > marxes.pop()
‘Zeppo’
marxes
[‘Groucho’, ‘Chico’, ‘Harpo’]
How to find index of Chico
»> marxes = [‘Groucho’, ‘Chico’, ‘Harpo’, ‘Zeppo’]
> > > marxes.index(‘Chico’)
1