Presentation is loading. Please wait.

Presentation is loading. Please wait.

Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.

Similar presentations


Presentation on theme: "Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries."— Presentation transcript:

1 Daniel Jung

2 Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries

3 Methods of List  list.append(item) Adds an item to the end of the list.  list.extend(List) Extends the list by appending all the items in the given list.  list.insert(i, item) Inserts an item at a given position. The argument i is the index of the element before which to insert.  list.remove(x) Removes the first item from the list whose value is x.

4 Methods of List (cont.)  list.pop(item) or list.pop() Removes the item at the given position in the list, and returns it. Removes and returns the last item in the list if no index is specified.  list.index(x) Returns the index in the list of the first item whose value is x.  list.count(x) Returns the number of times the value x appears in the list.  list.sort() Sorts the items of the list in order.  list.reverse() Reverses the order of elements of the list.

5 List as Stack  To add an item to the top of the stack, use the append(x).  To retrieve an item from the top of the stack, use pop().  Example: myStack = [1, 2, 3] myStack.append(4) Result: [1, 2, 3, 4] myStack.pop() Result: [1, 2, 3]

6 List as Queue  Possible, but not very efficient.  To implement a queue, type “from collections import deque”  Example: from collections import deque myQueue = deque([“Duke”, “Nukem”, “Is”]) myQueue.append(“Coming”) Result: [“Duke”, “Nukem”, “Is”, “Coming”] myQueue.popleft() Result: [“Nukem”, “Is”, “Coming”]

7 Useful Built-in Functions for Lists  filter(function, sequence) Returns a sequence consisting of those items from the sequence for which function(item) is true.  map(function, sequence) Calls function(item) for each of the items in the sequence and returns a list of the return values.  reduce(function, sequence) Returns a value constructed by calling the binary function on the first two items of the sequence, then on the result and the next item, and so on.

8 The del Statement  Unlike pop() method, it does not return a value.  To remove an item from a list: del myList[0]  To remove multiple items from a list: del myList[2:4]  To clear the entire list: del myList[:]  To delete entire variables: del myList

9 Tuple  Immutable – not possible to assign to the individual items  To create a tuple: myTuple1 = 666, 667, ‘six six eight’ Result: (666, 667, ‘six six eight’)  To create a nested tuple: myTuple2 = myTuple1, (1, 2, 3, 4) Result: ((666, 667, ‘six six eight’), (1, 2, 3, 4))  To create an empty tuple: emptyTuple = ()  To create a tuple with one item oneItemTuple = ‘tickle’,

10 Set  An unordered collection with no duplicate elements.  Basic uses Eliminating duplicate entries Membership testing  To create a set: myAnimals = [‘squirrel’, ‘fox’, ‘deer’, ‘squirrel’] mySet = set(myAnimals) Result: set([‘squirrel’, ‘fox’, ‘deer’])  Membership testing example: ‘squirrel’ in mySet Result: True ‘cat’ in mySet Result: False

11 Dictionary  Unlike sequences, dictionaries are indexed by keys.  Unordered set of key: value pairs  To create a dictionary: myDictionary = {‘toy’: ‘something to play with’, ‘food’: ‘something to eat’} dict([(‘toy’, ‘something to play with’), (‘food’, ‘something to eat’)])

12 The End  That is it!


Download ppt "Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries."

Similar presentations


Ads by Google