How to Multiply All Elements in a List With Python

One of the nice features common to many of the latest programming languages like Python is support for lambda functions and functional programming techniques. In Python, this is implemented with the "map" method, a powerful method that takes a function and a list, performs the function individually on every element in the list and returns the result. What once would have been a tedious for-loop is now a one-liner.

Instructions

    • 1

      Open a terminal. In Windows by clicking "Start" followed by "Run" and then typing "cmd." In Mac OS X, click "Spotlight" and type "Terminal."

    • 2

      Type "python" into the terminal.

    • 3

      Type the following line to define a list:

      list = [1,2,3,4,5,6,7,8,9,10]

    • 4

      Type the following line to multiply every element in the list by two:

      map(lambda x: x * 2, list)

      The "map" function performs a function on every element of a list. The "lambda" command defines a new, unnamed and temporary function for use in the map command and nowhere else. Combined, you can quickly create a new "times two" function and call it on every element in the list automatically.

Tips & Warnings

  • "Map" does not require a lambda function. Any valid Python function can be passed into "map." For example, try this to get all the ASCII values of a block of English text: map(ord, "Kevin")

Related Searches:

References

Comments

You May Also Like

  • How to Delete Elements From a Python List

    Python is a versatile high-level programming language that is much more flexible than older languages. One very useful aspect of the programming...

  • How to Append to a List in Python

    The Python programming language stores data in a variety of collections, including a list. The list collection stores a number of items...

  • How to List Functions in a Class in Python

    Python grants programmers the freedom to write code in whatever paradigm provides the best match of the project's requirements and the programmer's...

  • Python Lambda Tutorial

    Lambda expressions are a powerful feature in the Python programming language. However, they can be a little intimidating at first glance, especially...

  • How to Sort a Generic List

    Sorting a generic list is useful in generating a structured record of whatever items you may have in your list. This application...

  • What Is Lambda Max?

    Lambda max is the wavelength at which the maximum fraction of light is absorbed by a solution. Lambda (λ) is a Greek...

  • How to Get Rid of Stinkbugs in the House

    When stinkbugs invade your home, you know it. They hide in curtains, drawers, laundry or bedding. They get their name from the...

  • List of Values for Kids

    Children begin picking up behavioral cues from their parents and others at a very early age. Whether those behaviors are positive or...

  • Rules for Multiplying Exponents

    Rules for Multiplying Exponents. Many algebra problems use exponents when multiplying the same values together. Using exponents make it easier to write...

Related Ads

Featured