There are many ways of doing a conversion from a list to tuple in Python programing language. Lets understand each of them with examples.
Before we dive into conversion, let’s take a quick look at the list and tuple usage in Python.
List in Python
A list is an ordered collection of elements of various data types, or simply a sequence of data.
The list contains a variety of functions that are extremely useful in our programming.
It is mutable, which makes it more desirable than any other built-in data storage function. Some of these functions are as follows:
- Extend( )
Declaration of the List in Python
The code below demonstrates how to generate a list in Python, along with a brief explanation of each function’s purpose.
Note: When declaring a list in Python, square brackets [ ] are obligated.
# Declaration of list list = ['Beetechnical', 'is','1'] # output of list print (list) # use of function append list.append('Technical') #output for append print(list) # use of insert list.insert(2,"great") #output after insert print(list) # use of extend list.extend('2') print(list) # use of remove list.remove('great') #output After remove print(list) # use of pop list.pop(4) print(list)
As we can see, the list output contains square brackets and the data is the same as the input, and it is one of the identifications from the output for the list.
Tuple in Python
The built-in function tuple is a constant array used to store various types of variables, including int, char, float, and string.
A tuple is a time and space efficient despite being immutable and lacking in functions like list contain. Additionally, there are fewer run-time errors.
Declaration of Tuple in Python
The code below demonstrates how to generate a Tuple in Python, along with a brief explanation of each function’s purpose.
# declaring tuple tuple = 'Beetechnical','is','technical','site' # printing tuple print(tuple) # It gives maximum of data tuple have print(max(tuple)) #It gives minimum of variable tuple print(min(tuple)) # it gives index of mintioned variable tuple.count('is') tuple1 = 6,5,8,1,0 # for int type array print(sorted(tuple1)) # for sum of aal elements print(sum(tuple1))
Why Convert List to Tuple in Python
- To ensure that our function is error-free, we must transform a list into a tuple.
- For the same data, we might require both a list and a tuple.
- Instead of repeatedly overwriting it, we can merely define a list, and as we need a tuple, we can modify it by typecasting in the program. At some point in the program, we can list elements to add.
There are three ways to make a tuple out of a List.
- Utilizing built-in functionality,
- The looping method
- The unpacking technique
1. Utilizing built-in functionality
Python has a variety of built-in functions, as we all know. tuple() is one of these built-in functions. This function is used to typecast a list into a tuple. It is both incredibly simple and very helpful.
Due to its straightforward syntax, it is frequently used by programmers to typecast lists into tuples.
Below the code is provided as a demonstration for conversion :
# declare a list list = ['Beetechnical','is','technical','site'] # for type of function print(type(list)) print(list) # USE of built-in function tuple() tuple1 = tuple(list) # for type of function print(type(tuple1)) print(tuple1)
- In the code above, we create a list-type object to contain list literals before printing the type of object and the literals.
- With the tuple() method, we turn a list into a tuple, checking the object type using print type before printing literals.
- One simple way to distinguish between a list and a tuple is to look at their output differences. A list has [ ], whereas a tuple has ( ).
2. The Looping method
The only subtle difference between this method and the previous one is that we use a for loop inside a tuple() to iterate over each value in the list one by one before converting them to a tuple.
Below given code is a demonstration of loop conversion :
# declare a list list = ['Beetechnical','is','technical','site','part 2'] # for type of function print(type(list)) print(list) # USE of built-in function tuple() tuple1 = tuple(i for i in list) # for type of function print(type(tuple1)) print(tuple1)
3. The Unpacking technique
In this method, we use commas(,) to unpack list literals and asterisks (*) to pack them into tuples. Although this method is claimed to be less time-consuming, it has readability issues.
The Below code demonstrates the conversion of a list into a tuple by unpacking :
# declare a list list = ['Beetechnical','is','technical','site','part 3'] # for type of function print(type(list)) print(list) # USE of unpacking function tuple1 = (list,) tuple2 = (*list,) # for type of function print(type(tuple1)) print(tuple1) print(type(tuple2)) print(tuple2)
- As can be seen, the code above declares the list and its literals and prints the list’s type to confirm that the list and literals are present.
- After that, we use a comma (,) to unpack the list’s components and display their types and literals. We also pack the list’s elements into a tuple by an asterisk (*) to display their object types and literals.
- Although both methods display the object type tuple, we can observe the difference between them. Without an asterisk(*), the output is a tuple with a list inside of it.
These above three methods we directly implied but we can also use them with functions that make our work easier in lengthy programs instead of declaring, again and again, we can use function.
The below code is an illustration of this :
# function for conversion of list into tuple def converter(x): return tuple(x) list = ['Beetechnical','is','technical','site','part 3'] print(type(list)) # calling the function print(converter(list)) print(type(converter(list)))
This article explains how to create a tuple out of a list using the operator’s tuple () and comma (,). why it’s necessary to transform a list into a tuple.
We can conclude that each list and tuple has advantages and disadvantages. They serve as the necessary data for our program.