A
A
akokin2020-01-18 16:09:47
Python
akokin, 2020-01-18 16:09:47

What is the algorithm for fetching data from a Python list?

Colleagues, tell a beginner about a possible algorithm for retrieving (receiving) data from a Python list, which can contain both dictionaries, tuples, lists ...) using a loop. I have a misunderstanding with referring to nested structures, I just can’t understand the principle ....
For example, there is such a list:

s_list = [
    {'one': 1, 'two': 2, 'seven': 7, 'fix': 'price', 'number': [5, 4, 2, 3, 5, 4], 'dig': 4},
    {'one': 5, 'two': 4, 'seven': 6, 'fix': 'nix', 'number': [3, 5, 7, 2, 3, 9], 'dig': 5},
    {'one': 8, 'two': 3, 'seven': 9, 'fix': 'pix', 'number': [3, 2, 3, 1, 8, 4], 'dig': 9}
]

From this example I need to get values ​​by any key.
Just getting the value by key without a cycle in the first index is clear how: But how about in a cycle? How to iterate over all indexes in a list with for ?
print(s_list[0]['number'])
for x in s_list():
???

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-01-20
@akokin

s_list = [
    {'one': 1, 'two': 2, 'seven': 7, 'fix': 'price', 'number': [5, 4, 2, 3, 5, 4], 'dig': 4},
    {'one': 5, 'two': 4, 'seven': 6, 'fix': 'nix', 'number': [3, 5, 7, 2, 3, 9], 'dig': 5},
    {'one': 8, 'two': 3, 'seven': 9, 'fix': 'pix', 'number': [3, 2, 3, 1, 8, 4], 'dig': 9}
]
for i, item in enumerate(s_list):
    print('\nItem #', i, ':')    
    for key in item:
        print(key, '=', item[key])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question