콘텐츠로 건너뛰기

파이썬: 리스트 합계 구하는 방법

[

list sum python

Understanding the Summation Problem

  • Summing numeric values together is a fairly common problem in programming. For example, say you have a list of numbers [1, 2, 3, 4, 5] and want to add them together to compute their total sum. With standard arithmetic, you’ll do something like this:

1 + 2 + 3 + 4 + 5 = 15

  • As far as math goes, this expression is pretty straightforward. It walks you through a short series of additions until you find the sum of all the numbers.

  • It’s possible to do this particular calculation by hand, but imagine some other situations where it might not be so possible. If you have a particularly long list of numbers, adding by hand can be inefficient and error-prone. What happens if you don’t even know how many items are in the list? Finally, imagine a scenario where the number of items you need to add changes dynamically or unpredictably.

  • In situations like these, whether you have a long or short list of numbers, Python can be quite useful to solve summation problems.

Getting Started With Python’s sum()

  • Python’s built-in function sum() is an efficient and Pythonic way to sum a list of numeric values. Adding several numbers together is a common intermediate step in many computations, so sum() is a pretty handy tool for a Python programmer.

  • The Required Argument: iterable

    • The sum() function takes an iterable as its required argument. An iterable is any object in Python that can be looped over or iterated upon. Examples of iterables include lists, tuples, strings, and sets.
  • The Optional Argument: start

    • The sum() function has an optional second argument called start. This argument specifies the initial value to start the summation. If not provided, the default value is 0.

Summing Numeric Values

  • To find the sum of numeric values in a list using sum():
    numbers = [1, 2, 3, 4, 5]
    total = sum(numbers)

Concatenating Sequences

  • You can also use sum() to concatenate lists and tuples. This can be useful when you need to flatten a list of lists. For example:
    lists = [[1, 2], [3, 4], [5, 6]]
    concatenated = sum(lists, [])

Practicing With Python’s sum()

  • There are several other use cases for the sum() function. Here are a few examples:
    • Computing Cumulative Sums
    • Calculating the Mean of a Sample
    • Finding the Dot Product of Two Sequences
    • Flattening a List of Lists

Using Alternatives to sum()

  • While sum() is a powerful tool for summation, there are also alternative functions that can be used in specific scenarios:
    • Summing Floating-Point Numbers: math.fsum()
    • Concatenating Iterables With itertools.chain()
    • Concatenating Strings With str.join()

Conclusion

  • Python’s sum() function is a versatile tool for summing numeric values and concatenating sequences. It provides a simplified and efficient way to solve summation problems in your code. Understanding how to use sum() and its optional arguments can help you write more concise and Pythonic code.