nested

Multiply different size nested array with scalar

Multiply different size nested array with scalar Question: In my python code, I have an array that has different size inside like this arr = [ [1], [2,3], [4], [5,6,7], [8], [9,10,11] ] I want to multiply them by 10 so it will be like this arr = [ [10], [20,30], [40], [50,60,70], [80], [90,100,110] …

Total answers: 2

List comprehension with boolean (Python)

List comprehension with boolean (Python) Question: How to present this code as a list comprehension: def all_targets_hit(attempts: list) -> bool: for ls in range(len(attempts)): if not any(attempts[ls]): return False return True attempts =([ [True, False, False], [False, False, True], [False, False, False, False], ]) #Expected: False Asked By: avkpol || Source Answers: You could combine …

Total answers: 1

Get value from asymmetrically nested dict with unique keys

Get value from asymmetrically nested dict with unique keys Question: Say I have a nested dict, with asymmetrical nesting, and no key appearing more than once across the entire nested dict: d = { ‘d1’ : {‘d11’: ‘a’, ‘d12’: ‘b’}, ‘d2’ : {‘d21’: ‘c’, ‘d22’: {‘d221’: ‘d’, ‘d222’: ‘e’}} } I would like a function …

Total answers: 2

Using the same iterator in nested for loops

Using the same iterator in nested for loops Question: Consider the following code: from itertools import chain lst = [‘a’, 1, 2, 3, ‘b’, 4, 5, ‘c’, 6] def nestedForLoops(): it = iter(lst) for item0 in it: if isinstance(item0, str): print(item0) else: # this shouldn’t happen because of # 1. lst[0] is a str, and …

Total answers: 2

Get max values in a nested dictionary returning with the keys

Get max values in a nested dictionary returning with the keys Question: I’m new to Python and I need some help. I have this nested dictionary: diccionario = { "maria": {"valor1": 1, "valor2": 2} } And I want to extract the max value from the nested dictionary. I want this return: {"maria": valor2} I have …

Total answers: 4

Print nested list in a particular way

Print nested list in a particular way Question: I have this list: top_list = [[‘Recent news’, ’52’, ‘15.1’], [‘Godmorning’, ‘5’, ‘1.5’], [‘Sports news’, ’47’, ‘13.7’], [‘Report with weather’, ’34’, ‘9.9’], [‘The angel and the lawless’, ’33’, ‘9.6’], [‘Thundercats’, ‘3’, ‘0.9’], ["Mother’s legacy", ‘3’, ‘0.9’], [‘UR: Summer evenings with Europe of the Times’, ‘3’, ‘0.9’], [‘Tip’, …

Total answers: 1

in nested dictionary dynamically test if the value is a dictionary or a dictionary list

in nested dictionary dynamically test if the value is a dictionary or a dictionary list Question: I iterate through a nested dictionary taken from a json including one of the keys ("price") and sometimes a list sometimes a dictionary. Data={"main": {"sub_main": [ {"id": "995", "item": "850", "price": {"ref": "razorback", "value": "250"}}, {"id": "953", "item": "763", …

Total answers: 2

How do I get specific keys and their values from nested dict in python?

How do I get specific keys and their values from nested dict in python? Question: I need help, please be kind I’m a beginner. I have a nested dict like this: dict_ = { "timestamp": "2022-11-18T10: 10: 49.301Z", "name" : "example", "person":{ "birthyear": "2002" "birthname": "Examply" }, "order":{ "orderId": "1234" "ordername": "onetwothreefour" } } How …

Total answers: 2