1 / 13

Mastering Python Lists: Operations and Examples

Learn how to work with Python lists efficiently. Explore creation, manipulation, and advanced operations with examples. Dive into using lists as stacks, queues, filtering, mapping, and reducing. Discover sets and dictionaries in Python.

padkins
Download Presentation

Mastering Python Lists: Operations and Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Data Structures CS 265 Seth Simpson

  2. Lists • Creating Lists • exampleList = [1,3,5,7,9] • List methods • Append(element) – appends element to end • Extend(list) – extends the list with another list • Insert(position, element) – insert element at the given position • Remove(element) – removes the first occurrence of the element

  3. Lists (Cont) • More List methods • Pop(index) – Remove and return the element at index (last element by default) • Index(element) – returns index of the first occurrence of element • Count(element) – returns the number of times element appears in the list • Sort() – sorts the list • Reverse() – reverses the list

  4. Example of List Methods

  5. Lists as Stacks • Last in – First out • List pop function enables us to treat lists as stacks.

  6. Lists as Queues • First in – first out • Popping end elements of a list is fast because the elements don’t need to be rearranged • Popping from the front of a list is slow because the list needs to be rearranged

  7. List Filtering • Filter(function name, list) • Returns a sequence of items for which function(item) is true. • Example:

  8. List Mapping • Map(function name, list) • Calls the function for each value in the list and outputs the results as list • Example:

  9. List Reducing • Reduce(function name, list) • Returns a single value by computing the function value on the first two elements, then the result of that with the next element and so on.

  10. Sets • Unordered collection with no duplicates

  11. Sets (Cont)

  12. Dictionaries • Unsorted set of Key : Value pairs

  13. Dictionaries (Cont) • Can also call the dict() constructor to create a dictionary. • Printing out Dictionaries

More Related