Found 10784 Articles for Python

How to convert python tuple into a two-dimensional table?

karthikeya Boyini
Updated on 05-Mar-2020 06:10:12

1K+ Views

If you have a numeric library like numpy available, you should use the reshape method to reshape the tuple to a multidimensional array. exampleimport numpy data = numpy.array(range(1,10)) data.reshape([3,3]) print(data)OutputThis will give the output −array([[1, 2, 3],        [4, 5, 6],        [7, 8, 9]])ExampleIf you prefer to do it in pure python, you can use a list comprehension −data = tuple(range(1, 10)) table = tuple(data[n:n+3] for n in xrange(0,len(data),3)) print(table)OutputThis will give the output −((1, 2, 3), (4, 5, 6), (7, 8, 9))

How can I represent python tuple in JSON format?

Vikram Chiluka
Updated on 28-Oct-2022 11:24:45

21K+ Views

In this article, we will show you how to represent a tuple in python in JSON format. We will see the below-mentioned methods in this article: Converting Python Tuple to JSON Converting Python Tuple with Different Datatypes to JSON String Parsing JSON string and accessing elements using json.loads() method. Convert the dictionary of tuples to JSON using json.dumps() What is JSON? JSON (JavaScript Object Notation) is a simple lightweight data-interchange format that humans can read and write. Computers can also easily parse and generate it. JSON is a computer language that is based on JavaScript. It is a ... Read More

How can I remove items out of a Python tuple?

Samual Sam
Updated on 17-Jun-2020 10:12:50

1K+ Views

Tuples in python are immutable. If you want to remove items out of a Python tuple, you can use index slicing to leave out a particular index. For example,a = (1, 2, 3, 4, 5) b = a[:2] + a[3:] print(b)This will give the output:(1, 2, 4, 5)Or you can convert it to a list, remove the item and convert back to a tuple. For example,a = (1, 2, 3, 4, 5) ls_a = list(a) del ls_a[2] b = tuple(ls_a) print(b)This will give the output:(1, 2, 4, 5)

How can I create a Python tuple of Unicode strings?

Chandu yadav
Updated on 05-Mar-2020 06:06:15

145 Views

You can create a tuple of unicode strings in python using the u'' syntax when defining this tuple. examplea = [(u'亀',), (u'犬',)] print(a)OutputThis will give the output[('亀',), ('犬',)]Note that you have to provide the u if you want to say that this is a unicode string. Else it will be treated as a normal binary string. And you'll get an unexpected output. examplea = [('亀',), ('犬',)] print(a)OutputThis will give the output[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]

How can I create a non-literal python tuple?

Lakshmi Srinivas
Updated on 17-Jun-2020 10:11:15

128 Views

You can first construct a list then change the single value you want to change, then finally convert that to a tuple if you want to create a non-literal python tuple. For example,def create_non_literal_tuple(a, b, c):    x = [1] * a    x[c] = b    return tuple(x) create_non_literal_tuple(6, 0, 2)This will give the output:(1, 1, 0, 1, 1, 1)A 0 in position 2 of an array of length 6.

How can I represent immutable vectors in Python?

karthikeya Boyini
Updated on 05-Mar-2020 06:04:42

151 Views

You can use tuples to represent immutable vectors in Python. Tuples are immutable data structures that behave like a list but maintain order and are faster than lists. examplemyVec = (10, 15, 21) myVec[0] = 10This will produce an error as tuples can't be mutated.

How to group Python tuple elements by their first element?

Arjun Thakur
Updated on 05-Mar-2020 06:03:54

522 Views

Python has a function called defaultdict that groups Python tuple elements by their first element.Examplelst = [    (1, 'Hello', 'World', 112),    (2, 'Hello', 'People', 42),    (2, 'Hi', 'World', 200) ]from collections import defaultdictd = defaultdict(list) for k, *v in lst:    d[k].append(v) print(d)OutputThis will give the outputdefaultdict(, {1: [['Hello', 'World', 112]], 2: [['Hello', 'People', 42], ['Hi', 'World', 200]]})You can convert this back to tuples while keeping the grouping using the tuple(d.items()) method. exampleprint(tuple(d.items()))OutputThis will give the output((1, [['Hello', 'World', 112]]), (2, [['Hello', 'People', 42], ['Hi', 'World', 200]]))

How do we compare two tuples in Python?

Lakshmi Srinivas
Updated on 05-Mar-2020 05:57:34

6K+ Views

Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on. example>>> a = (1, 2, 3) >>> b = (1, 2, 5) >>> a < b TrueThere is another type of comparison that takes into account similar and different elements. This can be performed using sets. Sets will take the tuples and take only unique values. Then you can perform a & operation that ... Read More

How can I convert Python tuple to C array?

Vikram Chiluka
Updated on 09-Nov-2022 07:28:21

8K+ Views

In this article, we will show you how to convert a Python tuple to a C array in python. Python does not have a built-in array data type like other programming languages, but you can create an array by using a library like Numpy. Below are the various methods to convert a tuple into an array in Python − Using numpy.asarray() method Use numpy.array() method If you have not already installed NumPy on your system, run the following command to do so. pip install numpy Convert tuple to array using numpy.asarray() Use the np.asarray() function to convert ... Read More

Why python returns tuple in list instead of list in list?

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

285 Views

Python expects you to not mutate data when it returns some data. Tuples are also faster than lists. Tuples are generally used where order and position are meaningful and consistant. So for example, if you have a database driver in python and query for some data, you'll likely get back a list of tuples as the driver expects you to get the data and use it not mutate it. This also ensures that the data is in the same order as the fields you queried.

Advertisements