Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.

Similar presentations


Presentation on theme: "Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group."— Presentation transcript:

1 Data Structures in Python By: Christopher Todd

2 Lists in Python A list is a group of comma-separated values between square brackets. A list is a group of comma-separated values between square brackets. The values can be of multiple types: such as strings, ints, or other lists. The values can be of multiple types: such as strings, ints, or other lists. Lists start at the indices 0 Lists start at the indices 0 Defining a list: Defining a list: A = [ ‘hello’, 5, ‘test’, 8] A = [ ‘hello’, 5, ‘test’, 8] B = [ 0, 1, ‘test2’, A] B = [ 0, 1, ‘test2’, A]

3 Methods of a List len() len() Gives the size of the list Gives the size of the list append() append() Adds an item to the end of the list Adds an item to the end of the list extend(L) extend(L) Extend the list by appending all the items Extend the list by appending all the items remove(x) remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. Remove the first item from the list whose value is x. It is an error if there is no such item. pop([i]) pop([i]) Remove the item at the given position in the list Remove the item at the given position in the list

4 Methods of a List index(x) index(x) Return the index of the first item whose value is equal to x Return the index of the first item whose value is equal to x count(x) count(x) Return the number of times x is in the list Return the number of times x is in the list sort() sort() Sort the items of the list Sort the items of the list reverse() reverse() Reverse the items of the list Reverse the items of the list

5 Lists as Stacks List methods make it easy to use lists as stacks List methods make it easy to use lists as stacks The Stacks are formed with the last element being added is the first element retrieved The Stacks are formed with the last element being added is the first element retrieved (“last-in, first-out”) (“last-in, first-out”) Use append() to add to the top of the stack Use append() to add to the top of the stack Use pop() to retrieve from the top of the stack Use pop() to retrieve from the top of the stack

6 Lists as Queues Unlike sets, lists are not as efficient for forming Queues Unlike sets, lists are not as efficient for forming Queues Appending and popping from the front of the list is slower than doing the same from the back Appending and popping from the front of the list is slower than doing the same from the back The Queues are formed with the first element being added is the first element retrieved The Queues are formed with the first element being added is the first element retrieved (“first-in, first-out”) (“first-in, first-out”) Use collections.deque to implement a queue Use collections.deque to implement a queue This allows for faster access to both sides of the list This allows for faster access to both sides of the list

7 Tools when using Lists filter(function, sequence) filter(function, sequence) Returns a sequence consisting of items from the sequence that returns a true value for function(item) Returns a sequence consisting of items from the sequence that returns a true value for function(item) Outputs either a string, tuple, or list Outputs either a string, tuple, or list If sequence is a string or tuple, it returns data of the same type If sequence is a string or tuple, it returns data of the same type If sequence is anything else it returns a list If sequence is anything else it returns a list map(function, sequence) map(function, sequence) Calls function(item) for each of the sequence’s items Calls function(item) for each of the sequence’s items Returns a list of return values from the function(item) call Returns a list of return values from the function(item) call reduce(function, sequence) reduce(function, sequence) Returns a single value constructed by calling the first two items of a sequence, then the result and the next item until the sequence is spent Returns a single value constructed by calling the first two items of a sequence, then the result and the next item until the sequence is spent

8 List Comprehensions List comprehensions provide a concise way to create lists without resorting to the use of the aforementioned methods. List comprehensions provide a concise way to create lists without resorting to the use of the aforementioned methods. Each list comprehension consists of an expression followed by a for clause, then 0 or more for or if clauses. Each list comprehension consists of an expression followed by a for clause, then 0 or more for or if clauses. The result will be a list evaluated from the expression of the for and if clauses. The result will be a list evaluated from the expression of the for and if clauses. If the resulting list is to be converted into a Tuple, it must be parenthesized. If the resulting list is to be converted into a Tuple, it must be parenthesized. List comprehensions are much more flexible that map() and can be applied to complex expressions and nested functions List comprehensions are much more flexible that map() and can be applied to complex expressions and nested functions

9 Tools when using Lists filter(function, sequence) filter(function, sequence) Returns a sequence consisting of items from the sequence that returns a true value for function(item) Returns a sequence consisting of items from the sequence that returns a true value for function(item) Outputs either a string, tuple, or list Outputs either a string, tuple, or list If sequence is a string or tuple, it returns data of the same type If sequence is a string or tuple, it returns data of the same type If sequence is anything else it returns a list If sequence is anything else it returns a list map(function, sequence) map(function, sequence) Calls function(item) for each of the sequence’s items Calls function(item) for each of the sequence’s items Returns a list of return values from the function(item) call Returns a list of return values from the function(item) call reduce(function, sequence) reduce(function, sequence) Returns a single value constructed by calling the first two items of a sequence, then the result and the next item until the sequence is spent Returns a single value constructed by calling the first two items of a sequence, then the result and the next item until the sequence is spent

10 The del Statement The del statement is a way to remove an item from a list given its index, instead of its value The del statement is a way to remove an item from a list given its index, instead of its value The del statement can be used to remove slices from a list or clear an entire list The del statement can be used to remove slices from a list or clear an entire list del can also be used to delete variables del can also be used to delete variables del a del a If you reference the variable a after this, the program returns an error If you reference the variable a after this, the program returns an error Unlike pop(), del does not return a value Unlike pop(), del does not return a value

11 Tuples Tuples – ordered list separated by commas Tuples – ordered list separated by commas Usually used in databases and formatting Usually used in databases and formatting Some information about Tuples Some information about Tuples output Tuples are always enclosed in parentheses output Tuples are always enclosed in parentheses Tuples are immutable Tuples are immutable Defining Tuples Defining Tuples Empty Tuples: Empty Tuples: Empty = () Empty = () Tuples with one value: Tuples with one value: One = “hello”, One = “hello”,

12 Sets Sets – Unordered list that contain varying elements Sets – Unordered list that contain varying elements Usually used in membership testing and eliminating duplicate information Usually used in membership testing and eliminating duplicate information Some information about sets: Some information about sets: Cannot contain duplicate information Cannot contain duplicate information Set objects support mathematical operation Set objects support mathematical operation

13 Dictionaries Dictionaries – Unordered associative arrays Dictionaries – Unordered associative arrays Dictionaries indexed by keys which can be any immutable type such as int, string, or tuple Dictionaries indexed by keys which can be any immutable type such as int, string, or tuple You cannot use lists as keys You cannot use lists as keys Dictionary’s keys must be unique Dictionary’s keys must be unique Dictionaries are unordered sets with a key:value relationship Dictionaries are unordered sets with a key:value relationship

14 Dictionaries Cont. Defining Dictionaries: Defining Dictionaries: Use curly brackets {} Use curly brackets {} Separate keys and values by : Separate keys and values by : A) dictest = { ‘first’ : ‘1st’, ‘second’ : ‘2nd’} A) dictest = { ‘first’ : ‘1st’, ‘second’ : ‘2nd’} B) dict(first=1st, second=2nd) B) dict(first=1st, second=2nd) A and B create the same Dictionary A and B create the same Dictionary key() key() Returns all of the keys in the Dictionary Returns all of the keys in the Dictionary

15 Looping Techniques iteritems() iteritems() Used for printing the keys and values of a dictionary while looping Used for printing the keys and values of a dictionary while looping enumerate() enumerate() Used for printing the index and value of a list while looping Used for printing the index and value of a list while looping zip() zip() To loop over two or more lists at the same time To loop over two or more lists at the same time reverse() reverse() To loop over a list in reverse To loop over a list in reverse sorted() sorted() To loop over a list in sorted order To loop over a list in sorted order

16 Conditions The conditions used in while and if statements can contain any operators, not just comparisons. The conditions used in while and if statements can contain any operators, not just comparisons. Comparison operators have a lower priority than mathematical operators Comparison operators have a lower priority than mathematical operators Comparisons can be chained Comparisons can be chained a < b < c a < b < c Checks to see if a is less than b, than if b is less than c. Checks to see if a is less than b, than if b is less than c.

17 Conditions Cont. In & not in check to see if values exist in a list or sequence In & not in check to see if values exist in a list or sequence Is & is not check to see if two values are equal Is & is not check to see if two values are equal AND, OR, and NOT can all be used to combine multiple comparisons AND, OR, and NOT can all be used to combine multiple comparisons Parenthesis can be used for grouping Parenthesis can be used for grouping The operators are tested left to right The operators are tested left to right


Download ppt "Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group."

Similar presentations


Ads by Google