[Solved] Change the type between str and the name of list

I’m considering you have a string answer stored in the variable c and you want to access a list with the respective name. With this objective, you can use the python function eval(). From the python docs (here) you have that: eval(expression[, globals[, locals]]) The expression argument is parsed and evaluated as a Python expression … Read more

[Solved] Why do keys() and items() methods return different Boolean values for same key? (Python 3.6) [closed]

items() contains tuples, key-value pairs: >>> spam.items() dict_items([(‘name’, ‘Zophie’), (‘age’, 7)]) Your key is not such a tuple. It may be contained in one of the tuples, but in does not test for containment recursively. Either test for the correct key-value tuple: >>> (‘name’, ‘Zophie’) in spam.items() True or if you can’t get access to … Read more

[Solved] Python .join() format error

It was already said that the delimiter is inserted between each element not added to each element. However you can easily create a new list with the correctly added bear, for example: >>> lst = [‘brown’, ‘polar’, ‘grizzly’, ‘sun’, ‘kodiak’] >>> newlst = [item + ‘ bear’ for item in lst] >>> print(‘\n’.join(newlst)) brown bear … Read more

[Solved] Fastest (least execution time) approach to get max/min of an array of int without inbuilt functions except range() and len() [closed]

Fastest (least execution time) approach to get max/min of an array of int without inbuilt functions except range() and len() [closed] solved Fastest (least execution time) approach to get max/min of an array of int without inbuilt functions except range() and len() [closed]

[Solved] Dynamically build JSON in Python [closed]

You can create a helper function to dynamically populate the values in a dict object with the necessary structure: from __future__ import annotations def build_api_request(names: list[str], first: str, last: str, email: str, mobile_no: str, country_code: str | int): return { “list_name”: ‘,’.join(names), “subscriptions”: [ {“first_name”: first, “last_name”: last, “email”: email, “mobile”: {“number”: mobile_no, “country_code”: str(country_code)}} … Read more

[Solved] Parsing custom string

Very simple Use the str.split() function transform your alphabet soup, then split the single elements further by directly accessing them. For example: text=””‘ A1 A10 A10 B14 C1 C14 C14 C8 ”’ for t in text.split(): print( {t[0]: t[1:]} ) prints: {‘A’: ‘1’} {‘A’: ’10’} {‘A’: ’10’} {‘B’: ’14’} {‘C’: ‘1’} {‘C’: ’14’} {‘C’: ’14’} … Read more

[Solved] How to implement this Python code in Swift?

First you should do is to properly define type of the graph, because unlike Python you have to specify types in Swift in declaration: var graph: [String: [String: Int]] // Dictionary(hash table) with keys of type String and values of type Dictionary<String, Int> Then you should initialize graph with some initial value, because in Swift … Read more

[Solved] calculate and return the difference between the second largest number and the second smallest number [closed]

Here is a simple, but not most efficient way. you can achieve that by two step: convert list to set, to remove the duplicate number. use heap to find nlargest and nsmallest in set def difference(list1): set1 = set(list1) return heapq.nlargest(2, set1)[1] – heapq.nsmallest(2, set1)[1] Here is a one pass way, more efficient way, use … Read more