Chapter 7: Tuples And Lists Flashcards
A tuple is an ______ data type
Immutable
Creating an empty type
Empty_tuple = ()
Make a tuple with one or more elements
One_marx = ‘Groucho’,
One_marx = (‘Groucho’,)
If you have more than one element, follow all but the _____ one with a comma
Last
What is output
Marx_tuple = (‘Groucho’, ‘Chico’, ‘Harpo’)
A,b,c = marx_tuple
»>a
»>b
»>c
Groucho
‘Chico’
‘Harpo’
Marx_list = [‘Groucho’,’Chico’, ‘Harpo’]
Create a tuple using tuple()
Tuple(marx_list)
You can combine tuple using ____
+
You can duplicate items inside tuple using ____
*
Tuple iteration uses the keyword _____
In
Ex:
For word in words:
Print(word)
You _____ modify a tuple
Cannot
Lists in python are ______
Mutable
Create a list in python
First_last_name = [‘Tanvi’, ‘Menkurkar’]
What does the list() function do?
Converts other iterable data types (tuple, string, sets, dictionary) to lists.
Split()
Chops a string into a list by some separator
Ex:
Splitme = ‘a/b//c/d///e’
Splitme.split(‘/‘)
»>[‘a’,’b’,’ ‘, ‘c’, ‘d’, ‘’,’’,’e’]
What if you have more than one separator in your original string?
You get an empty string as an list item
How to get an item in a list?
List_name[offset]
How to get item from a list count from backward from the end?
Maxes[-1]
How to extract a subsequence of list
> > > Marxes[0:2]
[‘Groucho’,’Chico’]
How to reverse a list in python?
Reverse()
How to add item to end of list?
Append()
How to add item by offset with insert
List_name.insert(offset, item_name)
How to duplicate all item in a list using_____
*
How to combine lists
Option 1: extend()
Option 2: +
Ex:
Marxes += others
Marxes.extend(others)
How to change item at a certain index?
List_name[offset] = new_item
How to change a sub list in a list?
Slice
Numbers = [1,2,3,4]
Numbers[1:3] = [8,9]
> > > numbers
[1,8,9,4]
How to delete an item in a list by offset?
Del marxes[-1]
Del marxes[1]
Del is a python _____, not a list method
Statement
Delete an item with remove()
Marxes.remove(‘Groucho’)
How to delete an item (another way)
Pop(index)
How to delete all items
Clear()
How to find an item’s offset by value
Marxes.index(‘Chico’)
You can check if an item is in list using ___
In
‘Groucho’ in marxes
Find the number of occurrences in a list
.count()
Convert a list ot a string with ____
Join()
The list method ____ sorts the list itself, in place
Sort()
The general function _____ returns a sorted copy of the list
Sorted() —> does not modify original list
How to get length of list
Len()
When you assign one list to more than one variable using ____, changing the list in one place also changes it in the other
=
What are three ways to copy values of list to and independent, fresh list
- the list copy() method
The list() conversion function - THe list slice [:]
How to compare lists
Using ===, <=, >= and so on
How to iterate over multiple sequence in parallel
Zip() function
Ex:
For day,fruit,drink,dessert in zip(days,fruits,drinks, desserts):
Printf(….)
Zip() stops when the _____ sequence is done
Shortest
How to build a list of integers from 1 to 5
Number_list = list(range(1,6))
Another example
A_list = [number for number in range(1,60 if number %2 == 1]
Can you create a list of lists
Yes