Some Basic Python Tuple Programs-1


Tuple

A tuple is an immutable and sequential collection of data that can be indexed and sliced. Python tuple can contain homogeneous as well as heterogeneous values of data types such as integers, floats, strings, lists, and dictionaries.

Python Program to concatenate multiple tuples

  • The ‘+’ operator joins multiple tuples together.
  • The built-in sum() method adds multiple tuples and returns a newly formed tuple.
  • No original tuples are changed in these methods.

Python Program to modify a tuple

Once a tuple is created, its elements are unchangeable. However, overloading a tuple with existing tuple and change in the tuple is possible. There’s also another way to modify a tuple, converting the tuple into a list, modifying the list, and converting the list back into a tuple.


Python Program to Chunk Tuples

  • List comprehension is a compact method that defines a list and its contents at the same time according to some suitable valid expression.
  • The zip() built-in method returns an iterator of tuples, grouping the similar index of multiple sequences so that they can be used as a single entity.
  • The iter() function creates an iterator object. An iterable is an object which can return its element one by one.

Python Program to Flatten a Tuple of List to a Tuple

Flatten means the entire data object is mapped in one dimension.


Python Program to unique elements in a nested tuple

  • The extend() built-in list method adds each element of the iterable passed as an argument to the end of the list. The length of the list increases by the number of elements in the iterable.
  • The set() built-in method creates a set of the iterable passed. A set is an unordered collection of immutable and unique elements in python.

Python Program to Filter Tuples According to List Element Presence

  • In Python, list comprehension is mainly used to filter out unwanted elements by testing a suitable valid condition.
  • The and operation performs an intersection between the sets and returns a resulting set that contains only elements that are present in all of the specified sets. Python treats everything as true. However, null values, None, 0, empty data types are observed as false in python.

Add Comment

%d bloggers like this: