ordered list
list that cannot change order
changeable list
list that may change order or have added/removed items
indexed
set order from 0
list
multiple items in one variable
how to make a list
x=["a","b","c"] OR x=list(("a","b","c"))
what will len() do to lists
check how many items in the list
T/F: lists may contain any data type
True
T/F: lists may contain multiple data types
True
access "a" in the list mylist = list(("a","b","c"))
print(mylist[0])
access "c" in the list using negative index mylist = ["a","b","c"]
print(mylist[-1])
slice the list from the 2nd index to the 4th index mylist = ["a","b","c","d","e"]
print(mylist[2:5])
Change the 3rd item to "eggs" grocerylist = ["milk","sugar","berries"]
grocerylist[2] = "eggs"
Change all the items after the 3rd item to "apples", "berries"
fruits = ["peach","plum","pineapple","blueberry","pear"]
fruits [2:] = ["apples", "berries"]
What will happen to the list below
fruits = ["melon","orange","peach"] fruits[1:2] = "plum","apple","banana"
insert new items, move following items to fit.
fruits = ["melon","plum","apple","banana","peach"]
what will happen to the list below
fruits = ["melon","orange","peach"] fruits[1:3] = "plum"
replace items in boundary
fruits = ["melon","plum"]
Remove "nuts" using a string
party = ["ice cream","sprinkles","nuts"]
party.remove("nuts")
Remove "sprinkles" using "pop"
party = ["ice cream","sprinkles","nuts"]
party.pop(1)
what will the following lines do
party = ["ice cream","sprinkles","nuts"] party.pop() print(party)
["ice cream","sprinkles",]
Remove "ice cream" using "delete"
party = ["ice cream","sprinkles","nuts"]
party.delete[0]
what will the following lines do
party = ["ice cream","sprinkles","nuts"] del party print(party)
Error [Deletes list]
what will the following lines do
party = ["ice cream","sprinkles","nuts"] party.clear() print(party)
[BLANK] [Deletes items in list, not list itself]
loop the items in the list fruits = ["grapefruit","pineapple","kumquat"]
for x in fruits: print(x)
loop the items in the list by its length fruits = ["grapefruit","pineapple","kumquat"]
for x in range(len(x)): print(fruits[x])
loop the items in the list using its length fruits = ["grapefruit","pineapple","kumquat"]
x = 0 while x < len(fruits): print (fruits [x]) x += 1
loop the items in the list with a single line
[print(x) for x in fruits]
for the list "languages" add all asian languages to the following list languages = ["japanese","chinese","german","french"] translator = ?
translator = [x for x in languages if "ese" in x]
for the list "ores" add all items EXCLUDING "diamond" using the if condition ores = ["iron", "gold", "diamond", "lapis", "emerald"] inventory = ?
inventory = [x for x in ores if x != "diamond"]
what is the syntax for list comprehension
newlist = [ expression for item in iterable if condition == True]
what will happen in the following list comprehension inventory = ["D", "C", "B", "A" , "S"] myinventory = [x for x in inventory]
inventory duplicates into myinventory myinventory = ["D", "C", "B", "A" , "S"]
iterable
an object capable of returning its members one at a time.
return all the items in the list lowercased mylist = ["HAPPY", "sad", "Inspired"]
mylist_1 = [x.lower() for x in mylist]
return all items in the list as "null" mylist = ["player_data", "saves", "memory"]
mylist_1 = ["null" for x in mylist]
return all items in the list except those containing less than 5 characters to which replace them with "INCREASE_LENGTH" mylist = ["superfluous", "goad", "schadenfreude", "ubiquitous", "din"]
mylist_1 = [x if len(x)> 5 else "INCREASE_LENGTH" for x in mylist]
Arrange the list alphabetically in ascending order mylist = ["apple", "orange", "grapefruit", "peach"]
mylist.sort()
Arrange the list alphabetically in descending order mylist = ["apple", "orange", "grapefruit", "peach"]
mylist.sort(reverse = True)
Arrange the list by character count in ascending order mylist = ["chocolate", "Brazil", "dance"]
def mykey(x): return len(x)
mylist.sort(key = mykey)
What kind of strings are sorted first
Capitalized strings
sort the list alphabetically in ascending order regardless of capitalization mylist = ["apple", "orange", "grapefruit", "peach"]
mylist.sort(key = str.lower())
arrange the list backwards, disregarding alphabetization mylist = ["apple", "orange", "grapefruit", "peach"]
mylist.reverse()
add the string "midnights" to the end of the list ts = ["lover", "folklore", "evermore"]
ts.append("midnights")
add the string "1989" as the 2nd item ts = ["speak_now","red","reputation"]
ts.insert(1, "1989")
append the items from the former list to the latter new = ["btc", "lh"] existing = ["lush", "rfascib", "bmamc", "puberty_2"]
existing.extend(new)
what are the issues with list_1 = list_2 when copying lists
changes to one list will affect the other
copy the list from the former list to the latter movie = ["appreciative","nature_lover","romantic"] personality = ?
personality = list(movie) OR personality = movie.copy()
join two list even_numbers = [0, 2, 4] odd_numbers = [1, 3, 5]
all_numbers = even_numbers + odd_numbers OR even_numbers.extend(odd_numbers)
join two list by appending each item individually from the former list to the latter even_numbers = [0, 2, 4] odd_numbers = [1, 3, 5]
for x in even_numbers odd_numbers.append(x)