Lists Flashcards

1
Q

Creating lists

A

List = [1,2,3]

Blist = list (4,5,6)

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

Converting to lists

A

List(‘cat’)
»> [‘c’, ‘a’, ‘t’]

Or
List(a_tuple). ->. Creates list of values in a_tuple

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

Splitting function

A

Birthday = ‘1/6/1952’

Birthday.split(‘/’)
»> [‘1’, ‘6’, ‘1952’]

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

Get a list item by using [offset]

Change value by offset

A

Starts at zero

List[2] = ‘b’
»> changes the 3rd value of list to ‘b’

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

Get a slice to extract items by offset range

A

List[::2]. ->. Starts at list beginning and goes by 2

List[1:8:3]. ->. Goes from item 2 to item 9, by 3’s

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

Appending lists

A

List.append(‘value’)

-> appends ‘value’ to end of list

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

Extend function

A

Merges one list with another

A = [1,2,3]
B = [6,7,8]

A.extend(B)
»> [1,2,3,6,7,8]

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

”+=”

A

Functions like extend

Merges the items in the lists
(As opposed to append, which would have added the second list as a single list item

A=[1,2]. B=[3,4]
A += B. ->. [1,2,3,4]
A.append(B). ->. [1,2,[3,4]]

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

Insert() function

A

Adds an item before any offset in a list.

List.insert(3, ‘itema’)
Adds ‘itema’ before item #4

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

Del [ ] function

A

Deletes an item by offset

Del list[2]. -> deletes the third item in list

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

Remove ( ) function

A

Removes that item wherever in the list

List.remove(‘cat’). ->. Removes ‘cat’

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

“LIFO”

A

Last in first out

Data structure or stack.

Append() followed by pop()

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

“FIFO”

A

First in, first out

Data structure or stack

Pop(0)

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

Index(. ) function

A

List.index(‘value ‘) -> tells what offset an item is

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

‘Value ‘ in list

A

Returns True if the value is in list, else False

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

Count(. ) function

A

Counts how many times a particular value occurs in a list.

List.count(‘value’)

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

Sort() function

A

Sorts the list in place, changes the list

List.sort()

List.sort(reverse =True). - > reverse sorts

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

Sorted() function

A

Returns a sorted copy of the list

List_sorted = sorted(list)

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

Len() function

A

Returns the number of items in a list

20
Q

Ways to copy lists

A
A = [1,2,3]
B = A.copy()
C = list(A)
D = A[:]

B, C, D are copies of A, changing A does not change the rest and vica versa

21
Q

Tuple

A

Immutable

Follow all items with a comma, except last item

22
Q

Tuple unpacking

A

Assigning multiple variables at once

A_tuple = (1,2,3)
a,b,c = A_tuple
-> a is 1, b is 2, etc

23
Q

Tuple() conversion function

A

Makes tuples of other things

Tuple(list)

24
Q

Dictionary

A

Created by placing curly brackets around comma separated key:value pairs

Dict = {key:value1, …}

25
Q

dict() function

A

Converts two-value sequences into a dictionary. The first item in the list is the key, the second item is the value.

Dictionary keys must be unique.

26
Q

Adding to a dictionary

A

Dict[‘added_value’] = ‘value’

27
Q

Update() dictionary function

A

Copies the keys and values of one dictionary into another.

Pythons.update(funny_others)

Adds dictionary funny_others to pythons

28
Q

Deleting an item by key with “del[]”

A

Del dict[‘key’]

29
Q

clear() function for dictionaries

A

Dict.clear()

Deletes all keys and values from a dictionary

30
Q

Test for a key by using in

A

Returns True or False

‘Chapman’ in pythons
True

31
Q

Get an item by [key]

A

Most common use of a dictionary.

Test for key in dictionary first, if present, use:

Dict[‘key’] -> returns value

32
Q

Dictionary get() function

A

Dict.get(‘key’) -> returns value

Can also provide an optional value, if key exists you get its value; if not, you get the optional value.

Pythons.get(‘Marx’, ‘not a python’)
Will return ‘not a python’

33
Q

Get all keys by using keys() function

A

Dict.keys()

Returns all keys

34
Q

Get all values by using values ()

A

Dict.values()

Returns values of all keys in dict

35
Q

Get all key:value pairs by using items ()

A

Dict.items()

Returns all key:value pairs

36
Q

Lists

A

Lists are mutable.

Made from zero or more elements, separated by commas, and surrounded by square brackets.

[1,2,3]

37
Q

Set()

A

Set is like a dictionary with its values thrown away, leaving only the keys.

Use set() function
Or
Enclose one or more comma separated values in curly brackets.

38
Q

zip() function

A

Iterates multiple sequences in parallel

Ex = creating an English French dictionary
English = []
French []
EF_Dict = dict(zip(English, French))

39
Q

range()

A

Generates number sequences

For x in range (0,3):
Print(x)

-> creates a sequence from 0 to 2

List(range(0,3)) -> crestes a list of numbers from 0 to 2

40
Q

Comprehensions

A

A comprehension is a compact way of creating a Python data structure from one or more iterators.

Can combine loops and conditional tests with a less verbose syntax.

41
Q

List comprehension

A

[expression for item in iterable]

number_list = [number for number in range(1,6)
creates a list from 1-5

[expression for item in iterable if condition]

a_list = [number for number in range(1,6) if number % 2 == 1]
creates a list of odd numbers between 1 and 5

42
Q

List comprehension for rows, cols, cells

A
rows = range(1,4)
cols = range(1,3)
cells = [(row, col) for row in rows for col in cols]
for cell in cells:
    print(cell)

or
for row, col in cells:
print(row, col)

43
Q

Dictionary comprehensions

A

{key_expression:value_expression for expression in iterable}

word =’letters’
letter_counts = {letter:word.count(letter) for letter in word}
creates a dictionary of each letter in “letters” and how many times each letter occurs.

44
Q

Set comprehension

A

{expression for expression in iterable}

a_set = {number for number in range(1,6) if number % 3==1}
returns a list of every third number

45
Q

Generator comprehensions

A

Tuples do not have comprehensions!

Creates a “generator comprehension”, returns a generator object.
number_thing = (number for number in range(1,6))
type(number_thing)

A generator can only be run once.

A generator is one way to provide data to an iterator.