Which of the following functions can be used with a tuple as well as a list?

Lists and Tuples in Python

by John Sturtz basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Lists and Tuples in Python

Lists and tuples are arguably Python’s most versatile, useful data types. You will find them in virtually every nontrivial Python program.

Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples. You’ll learn how to define them and how to manipulate them. When you’re finished, you should have a good feel for when and how to use these object types in a Python program.

Take the Quiz: Test your knowledge with our interactive “Python Lists and Tuples” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Python Lists Vs Tuples

In this article we will learn key differences between the List and Tuples and how to use these two data structure.

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.

Tuples in Python

Tuples are a lot like lists, and that's why we can define them in a pretty similar way as we did to define the lists. Simply put, a tuple is a sequence of data.

What makes them different from lists is that tuples are immutable, i.e., the data inside the tuples can't be modified, which is opposite in the case of lists. Other than this, tuples are very similar to lists and that would make it much easier for us to understand as we already know about lists. If you don't, we suggest you to first go through the List tutorial.

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:

list(zip(list))

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.

map(function, iterable)

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,)]

Python Tuples

Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.

Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.

Video liên quan

Postingan terbaru

LIHAT SEMUA