Python select from list of tuples

Hey, readers! In this article, we will be focusing on Python List of Tuples.

What is Python List and Tuple?

Python List is a data structure that maintains an ordered collection of mutable data elements.

list-name = [ item1, item2, ....., itemN]

The elements in the list are enclosed within square brackets [].

Python Tuple is an immutable data structure whose elements are enclosed within parenthesis [].

tuple-name = [item1, item2, ...., itemN]

Python List of Tuples

We can create a list of tuples i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect.

Example:

LT_data = [[1,2,3],['S','P','Q']] print["List of Tuples:\n",LT_data]

Output:

List of Tuples: [[1, 2, 3], ['S', 'P', 'Q']]

Python list of tuples using zip[] function

Python zip[] function can be used to map the lists altogether to create a list of tuples using the below command:

The zip[] function returns an iterable of tuples based upon the values passed to it. And, further, the list[] function would create a list of those tuples as an output from the zip[] function.

Example:

lst1 = [10,20,30] lst2 = [50,"Python","JournalDev"] lst_tuple = list[zip[lst1,lst2]] print[lst_tuple]

Output:

[[10, 50], [20, 'Python'], [30, 'JournalDev']]

Customized grouping of elements while forming a list of tuples

While forming a list of tuples, it is possible for us to provide customized grouping of elements depending on the number of elements in the list/tuple.

[element for element in zip[*[iter[list]]*number]]

List comprehension along with zip[] function is used to convert the tuples to list and create a list of tuples. Python iter[] function is used to iterate an element of an object at a time. The ‘number‘ would specify the number of elements to be clubbed into a single tuple to form a list.

Example 1:

lst = [50,"Python","JournalDev",100] lst_tuple = [x for x in zip[*[iter[lst]]]] print[lst_tuple]

In the above example, we have formed a list of tuples with a single element inside a tuple using the iter[] method.

Output:

[[50,], ['Python',], ['JournalDev',], [100,]]

Example 2:

lst = [50,"Python","JournalDev",100] lst_tuple = [x for x in zip[*[iter[lst]]*2]] print[lst_tuple]

In this example, two elements are contained inside a tuple to form a list of tuple.

Output:

[[50, 'Python'], ['JournalDev', 100]]

Python list of tuples using map[] function

Python map function can be used to create a list of tuples. The map[] function maps and applies a function to an iterable passed to the function.

Example:

lst = [[50],["Python"],["JournalDev"],[100]] lst_tuple =list[map[tuple, lst]] print[lst_tuple]

In this example, we have mapped the input list to the tuple function using map[] function. After this, the list[] function is used to create a list of the mapped tuple values.

Output:

[[50,], ['Python',], ['JournalDev',], [100,]]

Python list of tuples using list comprehension and tuple[] method

Python tuple[] method along with List Comprehension can be used to form a list of tuples.

The tuple[] function helps to create tuples from the set of elements passed to it.

Example:

lst = [[50],["Python"],["JournalDev"],[100]] lst_tuple =[tuple[ele] for ele in lst] print[lst_tuple]

Output:

[[50,], ['Python',], ['JournalDev',], [100,]]

Conclusion

By this, we have come to the end of the article. I hope you all have enjoyed learning this interesting concept of Python list of tuples.

Feel free to comment below, in case you come across any doubt.

References

  • Creating a list of tuples — StackOverflow

Lists and Tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type including the nothing type defined by the None Keyword.

Lists and Tuples are similar in most context but there are some differences which we are going to find in this article.

Syntax Differences

Syntax of list and tuple is slightly different. Lists are surrounded by square brackets [] and Tuples are surrounded by parenthesis [].

Example 1.1: Creating List vs. Creating Tuple

list_num = [1,2,3,4] tup_num = [1,2,3,4] print[list_num] print[tup_num]

Output:

[1,2,3,4] [1,2,3,4]

Above, we defined a variable called list_num which hold a list of numbers from 1 to 4.The list is surrounded by brackets []. Also, we defined a variable tup_num; which contains a tuple of number from 1 to 4. The tuple is surrounded by parenthesis [].

In python we have type[] function which gives the type of object created.

Example 1.2: Find type of data structure using type[] function

type[list_num] type[tup_num]

Output:

list tuple

Mutable List vs Immutable Tuples

List has mutable nature i.e., list can be changed or modified after its creation according to needs whereas tuple has immutable nature i.e., tuple can’t be changed or modified after its creation.

Example 2.1: Modify an item List vs. Tuple

list_num[2] = 5 print[list_num] tup_num[2] = 5

Output:

[1,2,5,4] Traceback [most recent call last]: File "python", line 6, in TypeError: 'tuple' object does not support item assignment

In above code we assigned 5 to list_num at index 2 and we found 5 at index 2 in output. Also, we assigned 5 to tup_num at index 2 and we got type error. We can't modify the tuple due to its immutable nature.

Note: As the tuple is immutable these are fixed in size and list are variable in size.

Available Operations

Lists has more builtin function than that of tuple. We can use dir[[object]] inbuilt function to get all the associated functions for list and tuple.

Example 3.1: List Directory

dir[list_num]

Output:

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Example 3.2: Tuple Directory

dir[tup_num]

Output:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

We can clearly see that, there are so many additional functionalities associated with a list over a tuple.We can do insert and pop operation, removing and sorting elements in the list with inbuilt functions which is not available in Tuple.

Size Comparison

Tuples operation has smaller size than that of list, which makes it a bit faster but not that much to mention about until you have a huge number of elements.

Example 5.1: Calculate size of List vs. Tuple

a= [1,2,3,4,5,6,7,8,9,0] b= [1,2,3,4,5,6,7,8,9,0] print['a=',a.__sizeof__[]] print['b=',b.__sizeof__[]]

Output:

a= 104 b= 120

In above code we have tuple a and list b with same items but the size of tuple is less than the list.

Different Use Cases

At first sight, it might seem that lists can always replace tuples. But tuples are extremely useful data structures

  1. Using a tuple instead of a list can give the programmer and the interpreter a hint that the data should not be changed.
     
  2. Tuples are commonly used as the equivalent of a dictionary without keys to store data. For Example, [['Swordfish', 'Dominic Sena', 2001], ['Snowden', ' Oliver Stone', 2016], ['Taxi Driver', 'Martin Scorsese', 1976]] Above example contains tuples inside list which has a list of movies.
     
  3. Reading data is simpler when tuples are stored inside a list. For example, [[2,4], [5,7], [3,8], [5,9]] is easier to read than [[2,4], [5,7], [3,8], [5,9]]

Tuple can also be used as key in dictionary due to their hashable and immutable nature whereas Lists are not used as key in a dictionary because list can’t handle __hash__[] and have mutable nature.

key_val= {['alpha','bravo']:123} #Valid key_val = {['alpha','bravo']:123} #Invalid

Key points to remember:

  1. The literal syntax of tuples is shown by parentheses [] whereas the literal syntax of lists is shown by square brackets [] .
  2. Lists has variable length, tuple has fixed length.
  3. List has mutable nature, tuple has immutable nature.
  4. List has more functionality than the tuple.

Video liên quan

Chủ Đề