Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.

Similar presentations


Presentation on theme: "Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json."— Presentation transcript:

1 Lists/Dictionaries

2 What we are covering Data structure basics Lists Dictionaries Json

3 Data Structures We have a lot of data – how do we store it efficiently? Usually store data on disk, and have the code run off of the data instead of hard-coding the data in the program Different types of data – how to interpret them properly? Json is a structured format to store data (used with dictionaries) Also, good data structures are important to accessing data in them efficiently

4 Lists Lists are objects that help us organize large amounts of information We focus on: Basics of lists Declaration and use of lists Functions for lists

5 Lists A list can store a collection of data of any size. 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 A list of size N is indexed from zero to N-1 scores The entire list has a single name Each value has a numeric index This list holds 10 values that are indexed from 0 to 9

6 Lists A particular value in a list is referenced using the list name followed by the index in brackets Lists start at index 0 and go to index n-1 (if the list has n elements) For example, the expression scores[2] refers to the value 94 (the 3rd value in the list) That expression represents a place to store a single integer and can be used wherever an integer object can be used

7 Lists For example, a list element can be assigned a value, printed, or used in a calculation : scores[2] = 89 scores[first] = scores[first] + 2 mean = (scores[0] + scores[1])/2 print(scores[5])

8 Lists The values held in a list are called list elements A list stores multiple values of the same type or mixed types. Therefore, we can create a list of integers, a list of characters, a list of string s, a list of Coin objects, etc. In Python, the list is a sequence defined by the list class. It contains the methods for creating, manipulating, and processing lists.

9 Lists Another way to depict the scores list: scores 79 87 94 82 67 98 87 list variable

10 Declaring Lists The scores array could be declared as follows: scores = [79, 87, 94, 82, 67, 98, 87] To create a list, you can use list’s constructor: list1 = [] # create an empty list list2 = [2, 3, 4] list3 = [“red”, “green”, “blue”] list4 = range(3,6) # create a list with elements 3, 4, 5

11 List is a Sequence Type String and list are sequence types in Python A string is a sequence of characters, while a list is a sequence of any elements The sequence operations for lists are the same as for strings Several Python built-in functions can be used with lists; such as len(list), max / min, sum

12 Bounds Checking An index used in a list reference must specify a valid element That is, the index value must be in range 0 to len(list)-1 Accessing a list out of bound is a common programming error that results in a run-time IndexError.

13 Bounds Checking For example, if list can hold 100 values, it can be indexed using the numbers 0 to 99 If the list size is 100, then the following reference will cause an exception to be thrown: It’s common to introduce off-by-one errors i = 0 while i <= len(list): print(list[i]) i++ problem

14 Accessing Lists len(L) returns the number of items in the list L[i] returns the item at index i (the first item has index 0 ) L[i:j] returns a new list, containing the objects between i and j. L[-1] can be used to access the last item in a list.

15 Examples of using Lists for item in L: # where L is a list print(item) list1 = [2, 3] list2 = [1, 9] list3 = list1 + list2 list3 # what gets printed? list1 = list2 = [] # both names will point to the same list list1 = [] list1 = list2 # both names will point to the same list list1 = [] list2 = [] # independent lists

16 Updating lists list = ['physics', 'chemistry', 1997, 2000] print("Value available at index 2: ”) print(list[2]) list[2] = 2001 print("New value available at index 2 : ”) print(list[2])

17 Inputting lists list1 = [] # create an empty list print(“Enter 10 numbers: “) for i in range(10) list1.append(input()) # add input to list # read numbers as a string from the console s = input(“Enter 10 numbers separated by spaces: “) items = s.split() # extract each item as a list list1 = [int(x) for x in items] # convert to ints

18 Searching Lists The in operator can be used to check if an item is present in the list: if value in L: print("list contains: ", value) # or you can do a search: def linearSearch(myList, key) # -1 if not in list for i in range(len(myList)): if key == myList[i]: return i return -1

19 Python includes following list methods list.append(obj) Appends object obj to list list.append(obj) list.count(obj) Returns count of how many times obj occurs in list list.count(obj) list.extend(seq) Appends the contents of seq to list list.extend(seq) list.index(obj) Returns the lowest index in list that obj appears list.index(obj) list.insert(index, obj) Inserts object obj into list at offset index list.insert(index, obj) list.pop(obj=list[-1]) Removes and returns last object or obj from list list.pop(obj=list[-1]) list.remove(obj) Removes object obj from list list.remove(obj) list.reverse()Reverses objects of list in place list.reverse() list.sort([func]) Sorts objects of list, use compare func if given list.sort([func])

20 Dictionaries Dictionaries map keys to values – each key only has one value Keys are unique Application: for each character, count the number of times it is in a string Keys are the characters, Value for each key is the number of times it appears in the string

21 Dictionary Example d = {‘a’: 5, ‘b’: 7} d[‘c’] = 8 print(d) # {‘a’: 5, ‘b’: 7, ‘c’: 8} print(‘m’ in d) # False

22 Json A structured format to save data Each element in the json file is a key/value pair (as in dictionaries!) – sample below: { "menu": { "id": "file", "value": "File", "popup": [ "v1": "New", "v2": "Open" ] }

23 Json - continued For example, the “id” key has a value of “file” and the “popup” key has a list of elements. { "menu": { "id": "file", "value": "File", "popup": [ "v1": "New", "v2": "Open" ] }


Download ppt "Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json."

Similar presentations


Ads by Google