Get the value of a specific key up to the specified index in the dictionary list in Python

I couldn't write an easy-to-understand title ...

li = [
  {'name': 'col_a', 'length': 10},
  {'name': 'col_b', 'length': 8},
  {'name': 'col_c', 'length': 6},
  {'name': 'col_d', 'length': 4},
  {'name': 'col_e', 'length': 2}
]

I want the total length up to the third item!

When

sum([d['length'] for d in li[:3]])

You can get it with.

for idx in range(len(li)):
  print(sum(d.get('length', 0) for d in li[:i]]))

Like.

If you need a starting element, you can adjust it within the range.

Everyone loves it (?) For supporting fixed-length files, etc ...

Recommended Posts

Get the value of a specific key up to the specified index in the dictionary list in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
How to get a list of files in the same directory with python
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Get index of nth largest / smallest value in list in Python
Try to get a list of breaking news threads in Python.
How to check the memory size of a dictionary in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
I tried to create a Python script to get the value of a cell in Microsoft Excel
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Get the caller of a function in Python
Make a copy of the list in Python
[Linux] Command to get a list of commands executed in the past
I measured 6 methods to get the index of the maximum value (minimum value) of the list
How to pass the execution result of a shell command in a list in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
How to write a list / dictionary type of Python3
Python script to get a list of input examples for the AtCoder contest
[Introduction to Python] How to get the index of data with a for statement
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
How to store Python function in Value of dictionary (dict) and call function according to Key
How to identify the element with the smallest number of characters in a Python list?
A memo when checking whether the specified key exists in the defined dictionary with python
How to check in Python if one of the elements of a list is in another list
[python] How to check if the Key exists in the dictionary
Developed a library to get Kindle collection list in Python
Get the index of each element of the confusion matrix in Python
Extract the value closest to a value from a Python list element
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to check if the contents of the dictionary are the same in Python by hash value
How to pass the execution result of a shell command in a list in Python (non-blocking version)
Output the key list included in S3 Bucket to a file
How to determine the existence of a selenium element in Python
I tried to get the index of the list using the enumerate function
How to check the memory size of a variable in Python
[python] Get the rank of the values in List in ascending / descending order
How to get the vertex coordinates of a feature in ArcPy
[Python] How to get a value with a key other than value with Enum
Create a function to get the contents of the database in Go
How to delete multiple specified positions (indexes) in a Python list
Get a list of files in a folder with python without a path
[Python] A program that rotates the contents of the list to the left
Get the number of readers of a treatise on Mendeley in Python
Display a list of alphabets in Python 3
How to get a stacktrace in python
[python] Get a list of instance variables
Get the EDINET code list in Python
[Python] Get a list of folders only
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
Get a capture of the entire web page in Selenium Python VBA
Get a list of packages installed in your current environment with python
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get a value from a parameter store in lambda (using python)
Test & Debug Tips: Create a file of the specified size in Python
I want to sort a list in the order of other lists