You could just iterate over the indices of the range of the len of your list:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for index in range(len(dataList)):
for key in dataList[index]:
print(dataList[index][key])
or you could use a while loop with an index counter:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
index = 0
while index < len(dataList):
for key in dataList[index]:
print(dataList[index][key])
index += 1
you could even just iterate over the elements in the list directly:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
for key in dic:
print(dic[key])
It could be even without any lookups by just iterating over the values of the dictionaries:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
for val in dic.values():
print(val)
Or wrap the iterations inside a list-comprehension or a generator and unpack them later:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
print(*[val for dic in dataList for val in dic.values()], sep='\n')
the possibilities are endless. It's a matter of choice what you prefer.
Answer from MSeifert on Stack OverflowVideos
You could just iterate over the indices of the range of the len of your list:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for index in range(len(dataList)):
for key in dataList[index]:
print(dataList[index][key])
or you could use a while loop with an index counter:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
index = 0
while index < len(dataList):
for key in dataList[index]:
print(dataList[index][key])
index += 1
you could even just iterate over the elements in the list directly:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
for key in dic:
print(dic[key])
It could be even without any lookups by just iterating over the values of the dictionaries:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
for val in dic.values():
print(val)
Or wrap the iterations inside a list-comprehension or a generator and unpack them later:
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
print(*[val for dic in dataList for val in dic.values()], sep='\n')
the possibilities are endless. It's a matter of choice what you prefer.
use=[{'id': 29207858, 'isbn': '1632168146', 'isbn13': '9781632168146', 'ratings_count': 0}]
for dic in use:
for val,cal in dic.items():
print(f'{val} is {cal}')
When you iterate over a list, you get the elements, not the indices. Consider
for car in cars:
for x, y in car["coded"].items():
print(x, y)
i in this iteration is going to be
{
"name": "Ford",
"type": "SHORT",
"coded": {
"1": "Escape",
"2": "Fiesta",
"999": "Gumbo"
}
}
and
{
"name": "Honda",
"type": "SHORT",
"coded": {
"1": "Civic",
"2": "CRV",
"VCR": "Accord"
}
}
and not 1 and 2 as you expected anyways a better way of doing this is going to be
cars= [
{
"name": "Ford",
"type": "SHORT",
"coded": {
"1": "Escape",
"2": "Fiesta",
"999": "Gumbo"
}
},
{
"name": "Honda",
"type": "SHORT",
"coded": {
"1": "Civic",
"2": "CRV",
"VCR": "Accord"
}
}
]
for i in cars:
for x, y in i['coded'].items():
print(x, y)
You could use a generator to only grab ages.
# Get a dictionary
myList = [{'age':x} for x in range(1,10)]
# Enumerate ages
for i, age in enumerate(d['age'] for d in myList):
print i,age
And, yeah, don't use semicolons.
Very simple way, list of dictionary iterate
>>> my_list
[{'age': 0, 'name': 'A'}, {'age': 1, 'name': 'B'}, {'age': 2, 'name': 'C'}, {'age': 3, 'name': 'D'}, {'age': 4, 'name': 'E'}, {'age': 5, 'name': 'F'}]
>>> ages = [li['age'] for li in my_list]
>>> ages
[0, 1, 2, 3, 4, 5]
Hello everyone!
I have been unable to get the values out of a list of dictionaries with python. I've tried many things but nothing that is actually useful.
I have:
my_list = [
{ name: 'alex',
last_name: 'leda'
}
{ name: 'john',
last_name: 'parsons'
}
]I want to be able to loop through all dictionaries of the list and extract both the key and its corresponding value. Any idea as to how I would be able to accomplish this?
Many thanks!
Try r/learnpython for learning the language. This sub is more focused on news according to the sidebar.
my_list = [
{'name': 'alex', 'last_name': 'leda'},
{'name': 'john', 'last_name': 'parsons'}
]
for person in my_list:
for k, v in person.items():
print('{}: {}'.format(k, v))
How can I loop through a list and basically make a dictionary/hashtable with the list items as keys? I was looking at a leetcode solution for finding out which number in a list is a single number. Here's the code :
from collections import defaultdict
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hash_table = defaultdict(int)
for i in nums:
hash_table[i] += 1
for i in hash_table:
if hash_table[i] == 1:
return iI was wondering if there was an easier way of doing this without using collections and defaultdict