Python - Error , iterating list of dictionaries

None

I get error iterating list of dictionaries custom_list - list of dictionaries each dictionary have 3 keys

for i in custom_list:
    for link, id, timestamp in i.items():
        print("id : ", id)
        print("link : ", link)
        print("timestamp : ",timestamp)

The error:

    for link, id, timestamp in i.items():
ValueError: not enough values to unpack (expected 3, got 2)

if i print ' i ' i can see i have 3 values example of ' i ' dictionary print

{'id': 1, 'link': 'https://www.link.com/', 'timestamp': '2022-03-25 01:11:11.11111111'}
Aditya

i in the first for loop will give a dictionary so you can directly reference the key value pair without another for loop

for i in custom_list:
    print("id : ", i['id'])
    print("link : ", i['link'])
    print("timestamp : ",i['timestamp'])

If the dictionary's structure is dynamic and you want to iterate for all the key value pairs in the dictionary of current iteration you can add the following

for i in custom_list:
    for key, val in i.items():
        print(f'{key}: {value}')

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Python - Traverse list of dictionaries - Error as NoneType

Python: Dictionary with list of dictionaries

Iterate through list of dictionaries in python

Python: How to copy a list of a dictionaries

Problems removing dictionaries from list in Python

parsing 3 lists into list of dictionaries in python

How to quickly convert from items in a list of lists to list of dictionaries in python?

Filter a list of dictionaries by keys with different value for each key in Python

Python: How to create a csv string (no file) from a list of dictionaries?

How to sort a large list of dictionaries without loading into memory in Python

Python Group and aggregate unidirectionally a list of dictionaries by multiple keys

Iterating through a list of a class

Iterating through subelements of a list

Iterating a generic list with super keyword

Iterating through a list and querying a value

Django template not iterating through list

Extracting a single file from a zip archive without iterating over the entire name list in Python

Error with concatenating list to string in Python

Printing out dictionaries in Python

creating dictionaries in python

Distribute list of dictionaries by key evenly

Python - create new list containing multiple dictionaries with the same key name and different value

Python: comparing two iterating tables

Is it correct to append to a list while iterating over it?

Clojure Iterating over a list of hash maps

Multiplying polynomials in Python with the use of dictionaries

Iteration Through tuple of dictionaries in Python

Python : javascript error : missing) after argument list

"IndexError: list index out of range" error in python