0% found this document useful (0 votes)
4 views3 pages

Test Review - Lists

The document provides an overview of lists in Python, detailing their creation, properties, and functions. It explains how to manipulate lists, including indexing, concatenation, and methods specific to lists. Additionally, it covers complex lists and provides examples for better understanding, while noting that string methods will not be included in the test.

Uploaded by

czerillo2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Test Review - Lists

The document provides an overview of lists in Python, detailing their creation, properties, and functions. It explains how to manipulate lists, including indexing, concatenation, and methods specific to lists. Additionally, it covers complex lists and provides examples for better understanding, while noting that string methods will not be included in the test.

Uploaded by

czerillo2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Review – Lists

***The focus of this test will be on Chapters 13 & 14 in CS Circles***

LISTS
General

Lists are created using square brackets  L= [element1, element2, element3, … ]

Lists can store any data type as their elements.

Lists are a sequential data type, order is important.

Lists can be indexed  L[3]

- This is very similar to strings

Lists are mutable, strings are immutable.

- This means you can dynamically change certain elements in a list.


- L[0] = 5, will change the 0 index of L to be 5.

Functions

Some functions that work with list arguments:

len(list)  returns the number of elements in the list

sum(list)  returns the sum of all elements in the list

max(list)  returns the maximum element in the list

Concatenation

We can add lists together using + or we can use * to multiply many lists together

Examples:

L = [5,6]

S = [3,2]

L + S  [5,6,3,2]

L*3  [5, 6, 5, 6, 5, 6]
Loops and Lists

for x in list:

This allows us to step through a list, with x taking on the next element in the list.

Complex Lists (lists inside lists)

Recall lists can store any data type, including other lists.

To access data inside a list that is inside a list, first we need to index the outer list, then the inner list.

Example:

L = [ 10, 6, [4, 7, 9] ]

If we call the 2nd index of L, you will access the whole list.

L[2]  [4, 7, 9]

If we then take the index 1 of that it will access the element at location [1] of the list, L[2].

L[2][1]  7

Here is some code to demonstrate:

'''List Review'''
##mylist = ['a','b','c']
##print(mylist)

#2D lists and how to index


my2dList = [ [0,1,2] , ['a','b','c'] , [1.1,2.2,3.3] ]
print(my2dList)
print(my2dList[1])

print('the letter b from the above list')


print(my2dList[1][1])

###breakdown of what you see above


##temp = my2dList[1] #['a','b','c']
##temp[1] #--> 'b'

Methods

Methods are like functions, that work only on certain data types. A list method follows the list it will
work on. Methods have a period, then the name, then parentheses and arguments inside.

Listobject.methodname(arguments if any)

Know the following methods, how they work and what they return:
list.index(X) returns the index of the first occurrence of X in list
list.count(X) returns the number of occurrences of X in list
list.append(X) adds X to the end of the list
list.insert(i, X) adds X at position i
list.remove(X) removes the first occurence of X
list.pop(i) deletes & returns item list[i], while list.pop() deletes & returns the last item
list.reverse() reverses the list
list.sort() sorts the list

No string methods will be on this Test!!!

You might also like