Python Lists

5.0(1)
studied byStudied by 7 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/45

flashcard set

Earn XP

Description and Tags

From W3

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

46 Terms

1
New cards
ordered list
list that cannot change order
2
New cards
changeable list
list that may change order or have added/removed items
3
New cards
indexed
set order from 0
4
New cards
list
multiple items in one variable
5
New cards
how to make a list
x\=["a","b","c"]
OR
x\=list(("a","b","c"))
6
New cards
what will len() do to lists
check how many items in the list
7
New cards
T/F: lists may contain any data type
True
8
New cards
T/F: lists may contain multiple data types
True
9
New cards
access "a" in the list
mylist \= list(("a","b","c"))
print(mylist[0])
10
New cards
access "c" in the list using negative index
mylist \= ["a","b","c"]
print(mylist[-1])
11
New cards
slice the list from the 2nd index to the 4th index
mylist \= ["a","b","c","d","e"]
print(mylist[2:5])
12
New cards
Change the 3rd item to "eggs"
grocerylist \= ["milk","sugar","berries"]
grocerylist[2] \= "eggs"
13
New cards
Change all the items after the 3rd item to "apples", "berries"

fruits \= ["peach","plum","pineapple","blueberry","pear"]
fruits [2:] \= ["apples", "berries"]
14
New cards
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"]
15
New cards
what will happen to the list below

fruits \= ["melon","orange","peach"]
fruits[1:3] \= "plum"
replace items in boundary

fruits \= ["melon","plum"]
16
New cards
Remove "nuts" using a string

party \= ["ice cream","sprinkles","nuts"]
party.remove("nuts")
17
New cards
Remove "sprinkles" using "pop"

party \= ["ice cream","sprinkles","nuts"]
party.pop(1)
18
New cards
what will the following lines do

party \= ["ice cream","sprinkles","nuts"]
party.pop()
print(party)
\["ice cream","sprinkles",]
19
New cards
Remove "ice cream" using "delete"

party \= ["ice cream","sprinkles","nuts"]
party.delete[0]
20
New cards
what will the following lines do

party \= ["ice cream","sprinkles","nuts"]
del party
print(party)
Error
\[Deletes list]
21
New cards
what will the following lines do

party \= ["ice cream","sprinkles","nuts"]
party.clear()
print(party)
\[BLANK]
\[Deletes items in list, not list itself]
22
New cards
loop the items in the list
fruits \= ["grapefruit","pineapple","kumquat"]
for x in fruits:
print(x)
23
New cards
loop the items in the list by its length
fruits \= ["grapefruit","pineapple","kumquat"]
for x in range(len(x)):
print(fruits[x])
24
New cards
loop the items in the list using its length
fruits \= ["grapefruit","pineapple","kumquat"]
x \= 0
while x < len(fruits):
print (fruits [x])
x +\= 1
25
New cards
loop the items in the list with a single line
\[print(x) for x in fruits]
26
New cards
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]
27
New cards
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"]
28
New cards
what is the syntax for list comprehension
newlist \= [ expression for item in iterable if condition \== True]
29
New cards
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"]
30
New cards
iterable
an object capable of returning its members one at a time.
31
New cards
return all the items in the list lowercased
mylist \= ["HAPPY", "sad", "Inspired"]
mylist_1 \= [x.lower() for x in mylist]
32
New cards
return all items in the list as "null"
mylist \= ["player_data", "saves", "memory"]
mylist_1 \= ["null" for x in mylist]
33
New cards
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]
34
New cards
Arrange the list alphabetically in ascending order
mylist \= ["apple", "orange", "grapefruit", "peach"]
mylist.sort()
35
New cards
Arrange the list alphabetically in descending order
mylist \= ["apple", "orange", "grapefruit", "peach"]
mylist.sort(reverse \= True)
36
New cards
Arrange the list by character count in ascending order
mylist \= ["chocolate", "Brazil", "dance"]
def mykey(x):
return len(x)

mylist.sort(key \= mykey)
37
New cards
What kind of strings are sorted first
Capitalized strings
38
New cards
sort the list alphabetically in ascending order regardless of capitalization
mylist \= ["apple", "orange", "grapefruit", "peach"]
mylist.sort(key \= str.lower())
39
New cards
arrange the list backwards, disregarding alphabetization
mylist \= ["apple", "orange", "grapefruit", "peach"]
mylist.reverse()
40
New cards
add the string "midnights" to the end of the list
ts \= ["lover", "folklore", "evermore"]
ts.append("midnights")
41
New cards
add the string "1989" as the 2nd item
ts \= ["speak_now","red","reputation"]
ts.insert(1, "1989")
42
New cards
append the items from the former list to the latter
new \= ["btc", "lh"]
existing \= ["lush", "rfascib", "bmamc", "puberty_2"]
existing.extend(new)
43
New cards
what are the issues with list_1 \= list_2 when copying lists
changes to one list will affect the other
44
New cards
copy the list from the former list to the latter
movie \= ["appreciative","nature_lover","romantic"]
personality \= ?
personality \= list(movie)
OR
personality \= movie.copy()
45
New cards
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)
46
New cards
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)