chapter_three Flashcards

1
Q

What are lists and how do you name them?

A

A “list” is a collection of items in a particular order.
You can put anything you want into a list, and the items in your list don’t have to be related in any particular way.

Because a list usually contains more than one element, it’s good to make the name of your list plural.

In Python square brackets ( [ ] ) indicate a list, and individual elements in the list are seperated by commas.

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

How do i access elements in a list?

A

Lists are ordered collections, so you can access any element in a list by telling Python the position, or “index”, of the item desired.

To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.

example:
> list = [ ‘zero’, ‘one’, ‘two’ ]
> print( list[ 0 ] )
output: zero

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

list = [ ‘one’, ‘two’, ‘three’ ]

If I want to access the item ‘one’ in this list, which index position would I need to specifiy?

And how do I get the last item in a list of unknown lenght ?

A

Python considers the first item in a list to be at position 0, not 1.
This is true of most programming languages, and the reason has to do with how the list operations are implemented at lower level.

> list = [ ‘one’, ‘two’, ‘three’ ]
print( list[ 0 ] )
output: one

Python has special syntax for accessing the last element in a list. By asking for the item at index -1, Python always returns the last item in the list.

> list = [ ‘one’, ‘two’, ‘three’ ]
print( list[ -1 ] )
output: three

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

How do I change list elements?

A

To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want it to have.

> list = [ ‘no’ ]
list[ 0 ] = ‘yes’
print( list[ 0 ])
output: yes

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

What are the 2 methods to add an element to a list and how do they differ?

A

.append( )

The simplest way to add a new element to a list is to “append” the item to the list.
When you append an item to a list, the new element is added to the end of the list.

> list = [ ‘one’, ‘two’, ‘three’ ]
list.append( ‘four’ )
print( list )
output: [ ‘one’, ‘two’, ‘three’, ‘four’ ]

The append( ) method makes it easy to build lists dynamically.

.insert( )

You can add a new element at any position in your list by using the “insert( )” method. You do this by specifying the index of the new element and the value of the new item.

> list = [ ‘two’, ‘three’, ‘four’ ]
list.insert( 0, ‘one’ )
print( list )
output: [ ‘one’, ‘two’, ‘three’, ‘four’ ]

The “insert( )” method opens a space at the specifyied position and stores the value at that location. This operation shifts every other value in the list one position to the right.

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

What are the 2 ways to remove elements from a list?

How do they differ?

A

del

If you know the position of the item you want to remove from a list, you can use the del statement.
You can no longer access the value that was removed from the list after the “del” statement is used.

> list[ ‘mistakes’, ‘happen’ ]
del list[ 0 ]
print( list )
output: list[ ‘happen’ ]

.pop( )

The “pop( )” method removes the last item in a list, but it lets you work with that item after removing it.
You can use “pop( )” to remove an item from any list by including the index of the item you want to remove in parentheses.
Remember that each time you use pop( ), the item you work with is no longer stored in the list.

> list[ 'programming', '(q.q)' ]
> staring_human = list.pop( 1 )
> print( list )
> print( staring_human )
output: [ 'programming' ]
output: (q.q)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do I remove an item that I only know the value of?

What are the potential problems?

A

.remove( )

If you only know the value of the item you want to remove, you can use the “remove( )” method.
The “remove( )” method deletes only the first occurrence of the value you specify.
If there’s a posibility the value appears more than onve in the list, you’ll need to use a loop to make sure all occurrances of the value are removed

> list[ ‘bottle’, ‘cap’ ]
list.remove( ‘cap’ )
print( list )
output: [ ‘bottle’ ]

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

How do I sort lists permanently?

A

.sort( )

The “sort( )” method makes it relatively easy to sort a list and changes the order of the list permanently. We can never revert back to the original order.

example:
> list = [ 'c', 'b', 'a' ]
> list.sort( )
> print( list )
output: [ 'a', 'b', 'c' ]

You can also sort the list in reverse alphabetical order by passing the argument
“reverse=True” to the “sort( )” method.

example:
> list = [ 'a', 'b', 'c' ]
> list.sort( reverse=True )
> print( list )
output: [ 'c', 'b', 'a' ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do I sort a list temporarily?

A

sorted( )

To maintain the original of a list but present it in sorted order, you can use the “sorted( )” function.

The “sorted( )” function lets you display your list in a particular order but doesn’t affect the actual order of the list.

The “sorted( )” function can also accept a “reverse=True” argument if you want to display a list in reverse alphabetical order.

example:
> list = [ 'c', 'b', 'a' ]
> sorted_list = sorted( list )
> print( sorted_list )
> print( list )
output: [ 'a', 'b', 'c' ]
output: [ 'c', 'b', 'a' ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do I need to be careful of when I sort lists?

A

Sorting a list alphabetically is a bit more complicated when all the values are not in lowercase.

There are several ways to interpret capital letters when determining a sort order, and specifying the exact order can be more complex.

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

How do I reverse the original order of the list?

A

.reverse( )

To reverse the original order of a list, you can use the “reverse( )” method.

“reverse( )” doesn’t sort backwards alphabetically; it simply reverses the order of the list.
the “reverse” method changes the order of the list permanently, but you can revert to the original order anytime by applying “reverse( )” to the same list a second time.

example:
> list = [ 'three', 'two', 'one' ]
> list.reverse()
> print( list )
output: [ 'one', 'two', 'three' ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do I find the lenght of a list?

A

len( )

You can quickly find the lenght of a list by using the “len( )” function.

Python counts the items in a list starting with one, so you shouldn’t run into any off-by-one errors when determining the lenght of a list.

example:
> list = [ 'count', 'to', 'three' ]
> list_lenght = len( list )
> print( list_lenght )
output: 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

> list = [ ‘one’, ‘two’, ‘three’ ]
print( list[ 3 ] )

What is the problem here?

A

This will cause a IndexError to occure with the description: list index out of range.

The list has only a lenght of 3 which means the highest index you can access is 2.

An index error means Python can’t find an item at the index you requested. If an index error occurs in your program, try adjusting the index you’re asking for by one. Then run the program agian to see if the results are correct.

If an index error occurs and you can’t figure out how to resolve it, try printing your list or just printing the length of the list. Your list might look much different than your thought it did, especially if it has been managed dynamically by your program.

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