Python Lists

studied byStudied by 5 people
5.0(1)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 45

flashcard set

Earn XP

Description and Tags

From W3

46 Terms

1

ordered list

list that cannot change order

New cards
2

changeable list

list that may change order or have added/removed items

New cards
3

indexed

set order from 0

New cards
4

list

multiple items in one variable

New cards
5

how to make a list

x=["a","b","c"] OR x=list(("a","b","c"))

New cards
6

what will len() do to lists

check how many items in the list

New cards
7

T/F: lists may contain any data type

True

New cards
8

T/F: lists may contain multiple data types

True

New cards
9

access "a" in the list mylist = list(("a","b","c"))

print(mylist[0])

New cards
10

access "c" in the list using negative index mylist = ["a","b","c"]

print(mylist[-1])

New cards
11

slice the list from the 2nd index to the 4th index mylist = ["a","b","c","d","e"]

print(mylist[2:5])

New cards
12

Change the 3rd item to "eggs" grocerylist = ["milk","sugar","berries"]

grocerylist[2] = "eggs"

New cards
13

Change all the items after the 3rd item to "apples", "berries"

fruits = ["peach","plum","pineapple","blueberry","pear"]

fruits [2:] = ["apples", "berries"]

New cards
14

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"]

New cards
15

what will happen to the list below

fruits = ["melon","orange","peach"] fruits[1:3] = "plum"

replace items in boundary

fruits = ["melon","plum"]

New cards
16

Remove "nuts" using a string

party = ["ice cream","sprinkles","nuts"]

party.remove("nuts")

New cards
17

Remove "sprinkles" using "pop"

party = ["ice cream","sprinkles","nuts"]

party.pop(1)

New cards
18

what will the following lines do

party = ["ice cream","sprinkles","nuts"] party.pop() print(party)

["ice cream","sprinkles",]

New cards
19

Remove "ice cream" using "delete"

party = ["ice cream","sprinkles","nuts"]

party.delete[0]

New cards
20

what will the following lines do

party = ["ice cream","sprinkles","nuts"] del party print(party)

Error [Deletes list]

New cards
21

what will the following lines do

party = ["ice cream","sprinkles","nuts"] party.clear() print(party)

[BLANK] [Deletes items in list, not list itself]

New cards
22

loop the items in the list fruits = ["grapefruit","pineapple","kumquat"]

for x in fruits: print(x)

New cards
23

loop the items in the list by its length fruits = ["grapefruit","pineapple","kumquat"]

for x in range(len(x)): print(fruits[x])

New cards
24

loop the items in the list using its length fruits = ["grapefruit","pineapple","kumquat"]

x = 0 while x < len(fruits): print (fruits [x]) x += 1

New cards
25

loop the items in the list with a single line

[print(x) for x in fruits]

New cards
26

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]

New cards
27

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"]

New cards
28

what is the syntax for list comprehension

newlist = [ expression for item in iterable if condition == True]

New cards
29

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"]

New cards
30

iterable

an object capable of returning its members one at a time.

New cards
31

return all the items in the list lowercased mylist = ["HAPPY", "sad", "Inspired"]

mylist_1 = [x.lower() for x in mylist]

New cards
32

return all items in the list as "null" mylist = ["player_data", "saves", "memory"]

mylist_1 = ["null" for x in mylist]

New cards
33

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]

New cards
34

Arrange the list alphabetically in ascending order mylist = ["apple", "orange", "grapefruit", "peach"]

mylist.sort()

New cards
35

Arrange the list alphabetically in descending order mylist = ["apple", "orange", "grapefruit", "peach"]

mylist.sort(reverse = True)

New cards
36

Arrange the list by character count in ascending order mylist = ["chocolate", "Brazil", "dance"]

def mykey(x): return len(x)

mylist.sort(key = mykey)

New cards
37

What kind of strings are sorted first

Capitalized strings

New cards
38

sort the list alphabetically in ascending order regardless of capitalization mylist = ["apple", "orange", "grapefruit", "peach"]

mylist.sort(key = str.lower())

New cards
39

arrange the list backwards, disregarding alphabetization mylist = ["apple", "orange", "grapefruit", "peach"]

mylist.reverse()

New cards
40

add the string "midnights" to the end of the list ts = ["lover", "folklore", "evermore"]

ts.append("midnights")

New cards
41

add the string "1989" as the 2nd item ts = ["speak_now","red","reputation"]

ts.insert(1, "1989")

New cards
42

append the items from the former list to the latter new = ["btc", "lh"] existing = ["lush", "rfascib", "bmamc", "puberty_2"]

existing.extend(new)

New cards
43

what are the issues with list_1 = list_2 when copying lists

changes to one list will affect the other

New cards
44

copy the list from the former list to the latter movie = ["appreciative","nature_lover","romantic"] personality = ?

personality = list(movie) OR personality = movie.copy()

New cards
45

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)

New cards
46

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)

New cards
robot