Python List Comprehensions Explained

list comprehensions in python n.w
1 / 18
Embed
Share

Learn how to use list comprehensions in Python to create new lists based on existing lists. Understand the syntax, benefits, and examples of list comprehensions. Explore the power and popularity of this feature in Python programming.

  • Python
  • List Comprehensions
  • Programming
  • Syntax
  • Examples

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. List Comprehensions in Python By Dr. Ziad Al-Sharif

  2. List Comprehensions List Comprehensions A list comprehension is a programming language construct for creating a list based on existing lists Why Why comprehension comprehension ? ? The term is borrowed from math s set comprehension notation for defining sets in terms of other sets A powerful powerful and popular Generate a new list by applying a function to every member of an original list popular feature in Python Python s notation: [ [ expression expressionfor name namein list list ] ] [ expression for name in list ]

  3. List Comprehensions List Comprehensions The syntax of a list comprehension is somewhat tricky >>> L = [x-10 for x in grades if x>0] Syntax suggests that of a for-loop, an in operation, or an if statement All three of these keywords ( for , in , and if ) are also used in the syntax of forms of list comprehensions [ expression for name in list ]

  4. List Comprehensions List Comprehensions >>> li = [3, 6, 2, 7] >>> [elem*2 for elem in li] [6, 12, 4, 14] Note: Non-standard colors on next few slides clarify the list comprehension syntax. [ expressionfor name in list ] Where expression is some calculation or operation acting upon the variable name. For each member of the list, the list comprehension 1. sets name equal to that member, 2. calculates a new value using expression, It then collects these new values into a list which is the return value of the list comprehension. [ expression for name in list ]

  5. List Comprehensions List Comprehensions If list operate correctly on the types of all of list list contains elements of different types, then expression must list members. If the elements of list a container of names matching the type and shape of the list members. list are other containers, then name can consist of list Containers are objects that contain references to other objects (e.g., lists, types, dictionaries) >>> li = [( a , 1), ( b , 2), ( c , 7)] >>> [ n * 3 for (x, n) in li] [3, 6, 21] [ expression for name in list ]

  6. List Comprehensions List Comprehensions expression can also contain user-defined functions >>>defsubtract(a, b): return a b >>> oplist = [(6, 3), (1, 7), (5, 5)] >>> [subtract(y, x) for (x, y) in oplist] [-3, 6, 0] [ expression for name in list ]

  7. Syntactic sugar Syntactic sugar List comprehensions can be viewed as syntactic sugar for a typical higher-order functions [ expression for name in list ] map( lambda name: expression, list ) >>> L = [ 2*x+1 for x in [10, 20, 30] ] >>> L [21, 41, 61] >>> L = map( lambda x: 2*x+1, [10, 20, 30] ) >>> L <map object at 0x000001CEF8B581C0>

  8. Filtered List Comprehension Filtered List Comprehension Filter determines determines whether expression is performed on each member of the list. For each element of list, checks if it satisfies the filter condition. If the filter condition returns False, that element is omitted from the list before the list comprehension is evaluated. [ expression for name in list if filter]

  9. Filtered List Comprehension Filtered List Comprehension >>> li = [3, 6, 2, 7, 1, 9] >>> [elem*2 for elem in li if elem > 4] [12, 14, 18] Only 6 6, 7 7, and 9 9 satisfy the filter condition So, only 12 12, 14 14, and 18 18 are produce. [ expression for name in list if filter]

  10. More syntactic sugar More syntactic sugar Including an if clause begins to show the benefits of the sweetened form [ expressionfor name in list if filt ] map( lambda name . expression, filter(filt, list) ) >>> L=[ 2*x+1 for x in [10,20,30] if x > 0 ] >>> L [21, 41, 61] >>> L= map( lambda x: 2*x+1, filter(lambda x: x>0,[10,20,30] ) [ expression for name in list if filter]

  11. Nested List Comprehensions Nested List Comprehensions Since list comprehensions take a list as input and produce a list as output, they are easily nested The inner comprehension produces: [4, 3, 5, 2] So, the outer one produces: [8, 6, 10, 4] >>> li = [3, 2, 4, 1] >>> [elem*2 for elem in [item+1 for item in li] ] [8, 6, 10, 4] [ expression for name in list ]

  12. Syntactic sugar Syntactic sugar >>> [ e1 for n1 in [ e1 for n1 list ] ] >>> map( lambda n1: e1, map( lambda n2: e2, list ) ) >>> [2*x+1 for x in [y*y for y in [10, 20, 30]]] >>> map( lambda x: 2*x+1, map( lambda y: y*y, [10, 20, 30] )) [ expression for name in list ]

  13. Dict Comprehensions in Python By Dr. Ziad Al-Sharif

  14. Dict Dict Comprehensions Comprehensions PEP 202 PEP 202 introduces a syntactical extension to Python called the "list comprehension list comprehension". https://www.python.org/dev/peps/pep-0202/ PEP 274 PEP 274 proposes a similar syntactical extension called the "dictionary comprehension dictionary comprehension" or "dict dict comprehension comprehension" for short. https://www.python.org/dev/peps/pep-0274/ You can use dict comprehensions in ways very similar to list comprehensions, except that they produce Python dictionary objects instead of list objects.

  15. Dict Comprehensions Semantics >>> dict([(i, chr(65+i)) for i in range(4)]) {0: 'A', 1: 'B', 2: 'C', 3: 'D'} >>> is semantically equivalent to: >>> {i : chr(65+i) for i in range(4)}

  16. Examples Examples >>> d1 = {i:chr(65+i) for i in range(4)} >>> d1 {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'} >>> >>> d2 = {k+1:v for k,v in d1.items()} >>> d2 {1: 'A', 2: 'B', 3: 'C', 4: 'D'} >>> >>> d3 = {k:v.lower() for k, v in d2.items()} {1: 'a', 2: 'b', 3: 'c', 4: 'd'} >>>

  17. Examples Examples >>> def invert(d): ... return {v : k for k, v in d.items()} ... >>> d = {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'} >>> invert(d) {'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3} >>> d = {(k,v):k+v for k in range(4) for v in range(4)} >>> d {(0, 0): 0, (0, 1): 1, (0, 2): 2, (0, 3): 3, (1, 0): 1, (1, 1): 2, (1, 2): 3, (1, 3): 4, (2, 0): 2, (2, 1): 3, (2, 2): 4, (2, 3): 5, (3, 0): 3, (3, 1): 4, (3, 2): 5, (3, 3): 6 } >>>

  18. References Built-in Functions https://docs.python.org/3/library/functions.html Data Structures https://docs.python.org/3/library/functions.html When to Use a List Comprehension in Python https://realpython.com/list-comprehension-python/ Lambda Expressions https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions How to Use Python Lambda Functions https://realpython.com/python-lambda/ Chapter 4. Higher-Order Functions https://www.oreilly.com/library/view/functional-programming- in/9781492048633/ch04.html

More Related Content