Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Lists and Dictionaries

Similar presentations


Presentation on theme: "Chapter 5: Lists and Dictionaries"— Presentation transcript:

1 Chapter 5: Lists and Dictionaries
C10061 – Introduction to Computer Programming Kent State University Chapter 5: Lists and Dictionaries

2 Chapter Topics Covered
Lists vs. tuples List mutability List creation, lengths and operators List indexing and slicing New features of lists List methods Chapter 5: Lists and Dictionaries

3 Lists Lists are another type of sequence.
Similar to tuples and do everything tuples do Mutable (changeable) Use functions to find lengths Use operators to determine conditions Use indexing Use slicing Use methods for manipulation Chapter 5: Lists and Dictionaries

4 Lists Creating a list is the same as creating a
tuple, only the elements are enclosed in [ ]. Example: empty_list = [] Example: string_list = [“value1”, “value2”] number_list = [2.5,50] combo_list = [7,”string”] Chapter 5: Lists and Dictionaries

5 Lists Use len(list_name) to find how many elements exist in the list.
Use the in operator to check for conditions. Use indexing for sequential or random access. Use slicing with beginning and ending points. Chapter 5: Lists and Dictionaries

6 Lists: New Features Assigning a new value to an element
Note: Not applicable for creating new elements in the list (will generate an IndexError). Example: current_list = [“One”,Two”,“Three”] current_list[1] = “Zero” Result: ['One', 'Zero', 'Three'] Chapter 5: Lists and Dictionaries

7 Lists: New Features Assigning a new value to a slice
Note: Single list replaces a two-element slice. The number of elements decreases by 1. Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list[1:3] = “Zero” Result: ['One', 'Zero', 'Three'] Chapter 5: Lists and Dictionaries

8 Lists: New Features Deleting a list element using the del command
Note: The gap created by a deletion is filled automatically; all elements shift left one position. Example: current_list = [“One”,“Two”,“Three”,”Four”] del current_list[0] Result: [‘Two', ‘Three', ‘Four'] Chapter 5: Lists and Dictionaries

9 Lists: New Features Deleting a slice using the del command
Note: The gap created by a deletion is filled automatically; all elements shift left one position. Example: current_list = [“One”,“Two”,“Three”,”Four”] del current_list[:3] Result: ['Four'] Chapter 5: Lists and Dictionaries

10 Lists: append( ) Function
Add a new element to the end of the list Example: current_list = [“One”,“Two”,“Three”,”Four”] new_element = “Five” current_list.append(new_element) Result: ['One', 'Two', 'Three', 'Four', 'Five'] Chapter 5: Lists and Dictionaries

11 Lists: remove( ) Function
Remove the first occurrence of a value Example: current_list = [“One”,“Two”,“Two”,”Four”] remove_element = “Two” if remove_element in current_list: current_list.remove(remove_element) Result: ['One', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

12 Lists: sort( ) Function
Sorts the elements of the list in ascending order Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list.sort( ) Result: ['Four', 'One', 'Three', 'Two'] Chapter 5: Lists and Dictionaries

13 Lists: reverse( ) Function
Sorts the elements of the list in descending order Example: current_list = [“One”,“Two”,“Three”,”Four”] current_list.reverse( ) Result: ['Four', 'Three', 'Two', 'One'] Chapter 5: Lists and Dictionaries

14 Lists: count( ) Function
Returns the number of occurrences of a specific value Example: current_list = [“One”,“Two”,“Two”,”Four”] search = “Two” number = current_list.count(search) print number Result: 2 Chapter 5: Lists and Dictionaries

15 Lists: index( ) Function
Returns the first position number of where a specific value occurs Example: current_list = [“One”,“Two”,“Two”,”Four”] search = “Two” number = current_list.index(search) print number Result: 1 Chapter 5: Lists and Dictionaries

16 Lists: insert( ) Function
Inserts a specific value at a specific position Example: current_list = [“One”,“Two”,“Two”,”Four”] new = “Three” current_list.insert(2,new) Result: ['One', 'Two', 'Three', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

17 Lists: pop([i]) Function
Returns a value at a specific position (i) and removes the value from the list Example: current_list = [“One”,“Two”,“Two”,”Four”] new = current_list.pop(1) print new print current_list Result: Two ['One', 'Two', 'Four'] Chapter 5: Lists and Dictionaries

18 Lists: pop( ) Function from the list
Returns and removes the last element from the list Example: current_list = [“One”,“Two”,“Two”,”Four”] last = current_list.pop( ) print last print current_list Result: Four ['One', 'Two', ‘Two'] Chapter 5: Lists and Dictionaries


Download ppt "Chapter 5: Lists and Dictionaries"

Similar presentations


Ads by Google