W3 Basics - Lists Flashcards

1
Q

What is a namespace

A

Mapping from names to objects.

For instance, two modules might have an attribute/method with the same name, but they are in different namespaces

module1.example module2.example module3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

I’ve created an object from a class. What can I add to the class to make the output of the below to be “Hi, my name is Jeff”

obj = Class()
print(obj)

A

def __str__(self):
return “Hello, my name is Jeff”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Remove whitespace at beginning and end of string

A

string.strip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Replace H with J in Hello world

A

string.replace(‘H’, ‘J’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Turn string into a list separating elements by a ‘,’

A

string.split(‘,’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using an f string, turn x = 55 in x = 55.00

add numbers in an fstring

A

print(f”{x:.2f} {22 + 50}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give a word double quotes with an escape character

A

“We are "vikings" today”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using an if statement see if ‘apple’ is in a list

A

if ‘apple’ in list:

if ‘apple’ not in list:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Change second list item
If list has 6 elements, change the 2nd and 3rd item to ‘watermelon’ and ‘grape’

A

list[1] = ‘blue’
list[1:3] = [ ‘watermelon’, ‘grape’ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If you insert more elements then your replace, what is the output:

thislist = [“apple”, “banana”, “cherry”]

thislist[1:2] = [“blackcurrant”, “watermelon”]

A

[‘apple’, ‘blackcurrant’, ‘watermelon’, ‘cherry’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Add list item ‘watermelon’ to a list with three entries. It should go in the second spot and erase the third entry

add ‘watermelon’ to the third spot using a method

A

list[1:3] = [‘watermelon’]

list.insert(2, ‘watermelon’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Append items in ‘tropical’ to ‘thislist’

A

thislist.extend(tropical)

If you append tuples or sets they’ll still come out as list items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

There are four ways to delete lists
via string
2 via index
deleting the list itself
deleting all the list’s contents

Perform all of these steps

A

list.remove(‘’)
list.pop(2)
list.pop() removes last item
del list[2]
del list
list.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Using some python skills, create a loop that iterates through all list indices. Remember, you want to do this via INDICES

A

list = [‘this’, ‘that’, ‘other’]

for i in range(len(list)):
print(list[])

OR

i = 0
while i < len(list):
print(list[i]
i += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Loop through a list using list comprehension

A

[print(x) for x in list]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Using list comprehension, create a new list that only contains items that contain ‘a’ from ‘list’

A

[x for x in list if ‘a’ in x]

longer way
newlist = []
for x in (list):
if ‘a’ in x:
newlist.append(x)

17
Q

Using list comprehension, create a list of numbers 0 - 9

Do one that runs the same command, except only have it add items that are less than 5

A

[x for x in range(10)]

[x for x in range(10) if x < 5]

18
Q

Using list comprehension, change ‘banana’ to ‘orange’ in:

fruits = [“apple”, “banana”, “cherry”, “kiwi”, “mango”]

A

[x if x != ‘banana’ else ‘orange’ for x in fruits]

19
Q

Put your list in alphabetical/numeric order
Next sort them in reverse alphabetical/numeric order (different then just .reverse())

A

list.sort()
list.sort(reverse = True)

20
Q

Sort the list based on how close the number is to 50:

thislist = [100, 50, 65, 82, 23]

A

def myfunc(n):
return abs(n - 50)

thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)

21
Q

Does sort sort capital or lower case letters first?

How would you use the sort method to turn everything into lowercase?

A

Capitals go before lowercase

list.sort(key = str.lower)

22
Q

Reverse the order of a list completely no matter the capitalization

A

list.reverse()

23
Q

Copy list into newlist
Do it via another method
Do it via a slice operator

A

newlist = list.copy()

mylist = list(list)

mylist = list[:]

24
Q

Add list2 to list1 in three different ways

A

list1.extend(list2)
list3 = list1 = list2
for i in list2:
list1.append(i)