Found 10805 Articles for Python

How can we read Python dictionary using C++?

Samual Sam
Updated on 30-Jul-2019 22:30:22

320 Views

There are many C++/Python bindings. It boils down to what you use to communicate between C++ and python to read python dictionaries in c++. Most of these libraries(like Boost) handle the parsing themselves. You could use an intermediate data transfer format like JSON or XML to pass data between the 2 languages and then serialize and deserialize data using the respective libraries in these languages for these formats.

How to sort a Python dictionary by datatype?

Lakshmi Srinivas
Updated on 17-Jun-2020 10:24:04

122 Views

You can sort a list of dictionaries by values of the dictionary using the sorted function and passing it a lambda that tells which key to use for sorting. For example, A = [{'name':'john', 'age':45},      {'name':'andi', 'age':23},      {'name':'john', 'age':22},      {'name':'paul', 'age':35},      {'name':'john', 'age':21}] new_A = sorted(A, key=lambda x: x['age']) print(new_A)This will give the output:[{'name': 'john', 'age': 21}, {'name': 'john', 'age': 22}, {'name': 'andi', 'age': 23}, {'name': 'paul', 'age': 35}, {'name': 'john', 'age': 45}]You can also sort it in place using the sort function instead of the sorted function. For example, A ... Read More

How to sort a nested Python dictionary?

karthikeya Boyini
Updated on 17-Jun-2020 10:20:50

2K+ Views

If you have a dictionary of the following format:{    'KEY1':{'name':'foo', 'data':1351, 'completed':100},    'KEY2':{'name':'bar', 'data':1541, 'completed':12},    'KEY3':{'name':'baz', 'data':58413, 'completed':18} }And you want to sort by the key, completed within each entry, in a ascending order, you can use the sorted function with a lambda that specifies which key to use to sort the data. For example, my_collection = {    'KEY1':{'name':'foo', 'data':1351, 'completed':100},    'KEY2':{'name':'bar', 'data':1541, 'completed':12},    'KEY3':{'name':'baz', 'data':58413, 'completed':18} } sorted_keys = sorted(my_collection, key=lambda x: (my_collection[x]['completed'])) print(sorted_keys)This will give the output:['KEY2', 'KEY3', 'KEY1']Read More

Do you think a Python dictionary is thread safe?

George John
Updated on 30-Jul-2019 22:30:22

1K+ Views

Yes, a Python dictionary is thread safe. Actually, all built-ins in python are thread safe. You can read moreabout this in the documentation: https://docs.python.org/3/glossary.html#term-global-interpreter-lock

How to optimize Python dictionary access code?

Samual Sam
Updated on 30-Jul-2019 22:30:22

191 Views

dicts in python are heavily optimized. Creating a dict from N keys or key/value pairs is O(N), fetching is O(1), putting is amortized O(1), and so forth. You don't need to optimize them explicitly. You can be sure of this as python under the hood implements its own classes using dicts.Don't compare lists/tuples to dicts/sets though as they solve different problems.

How to print a complete tuple in Python using string formatting?

Monica Mona
Updated on 17-Jun-2020 10:18:15

563 Views

When using the old style of string formatting in python, ie, "" % (), if the thing after the percent is a tuple, python tries to break it down and pass individual items in it to the string. For example, tup = (1, 2, 3) print("this is a tuple %s" % (tup))This will give the output:TypeError: not all arguments converted during string formattingThis is because of the reason mentioned above. If you want to pass a tuple, you need to create a wrapping tuple using the (tup, ) syntax. For example, tup = (1, 2, 3) print("this is a tuple ... Read More

How to convert an object x to an expression string in Python?

Chandu yadav
Updated on 05-Mar-2020 06:40:47

522 Views

The str function converts an object in python to a string representation. There is another function called repr() in python that converts object to an expression string. __repr__'s goal is to be unambigous while __str__'s is to be readable. __repr__ is used to compute the “official” string representation of an object.ExampleLet's take an example of datetime to understand what these 2 produce.import datetime today = datetime.datetime.now() str(today) repr(today)OutputThis will give the output'2018-04-08 11:25:36.918979' 'datetime.datetime(2018, 4, 8, 11, 25, 36, 918979)'As you can see from the output, str gives a pretty, formatted result. Repr just throws an object constructor representation at ... Read More

How can I convert a Python tuple to an Array?

karthikeya Boyini
Updated on 05-Mar-2020 06:39:23

3K+ Views

To convert a tuple to an array(list) you can directly use the list constructor. examplex = (1, 2, 3) y = list(x) print(y)OutputThis will give the output −[1, 2, 3]ExampleIf you have a multi-level tuple and want a flat array, you can use the following −z = ((1, 2, 3), (4, 5)) y = [a for b in z for a in b] print(y)OutputThis will give the output −[1, 2, 3, 4, 5]

How can I write an SQL IN query with a Python tuple?

Arjun Thakur
Updated on 05-Mar-2020 06:33:30

2K+ Views

To write an SQL in query, you need to ensure that you provide the placeholders in the query using  so that the query is properly escaped. For example,Examplemy_tuple = ("Hello", "world", "John") placeholder= '?' placeholders= ', '.join(placeholder for _ in my_tuple) query= 'SELECT name FROM students WHERE id IN (%s)' % placeholders print(query)# now execute using the cursorcursor.execute(query, my_tuple)OutputThis will give the output'SELECT name FROM students WHERE id IN (?, ?, ?)'And when you call to execute, it'll replace them? placeholders correctly by the escaped values.

How can I append a tuple into another tuple in Python?

Vikram Chiluka
Updated on 28-Oct-2022 08:11:33

8K+ Views

In this article, we will show you how to append a tuple into another in python. Below are the various methods to accomplish this task − Using + operator. Using sum() function. Using list() & extend() functions. Using the unpacking(*) operator. Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using + operator Algorithm (Steps) Following are the Algorithm/steps to be followed to ... Read More

Advertisements