Lists and Tuples in Python
Table of Contents
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 | Convert a list into a tuple
Given a list, write a Python program to convert the given list into a tuple.
Examples:
Input : [1, 2, 3, 4] Output : (1, 2, 3, 4) Input : ['a', 'b', 'c'] Output : ('a', 'b', 'c')
Approach #1 : Using tuple(list_name).
Typecasting to tuple can be done by simply using tuple(list_name).
# Python3 program to convert a
# list into a tuple
def convert(list):
return tuple(list)
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
(1, 2, 3, 4)
Approach #2 :
A small variation to the above approach is to use a loop inside tuple() .
# Python3 program to convert a
# list into a tuple
def convert(list):
return tuple(i for i in list)
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
Approach #3 : Using (*list, )
This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma (, ). This approach is a bit faster but suffers from readability.
# Python3 program to convert a
# list into a tuple
def convert(list):
return (*list, )
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
Python | Convert list of tuples into list
Given a list of tuples, write a Python program to convert it into list.
Examples:
Below are methods to convert list of tuples into list.
Method #1 : Using list comprehension
# Python code to convert list of tuples into list
# List of tuple initialization
lt = [('Geeks', 2), ('For', 4), ('geek', '6')]
# using list comprehension
out = [item for t in lt for item in t]
# printing output
print(out)
|
['Geeks', 2, 'For', 4, 'geek', '6']
Method #2 : Using itertools
# Python code to convert list of tuple into list
# Importing
import itertools
# List of tuple initialization
tuple = [(1, 2), (3, 4), (5, 6)]
# Using itertools
out = list(itertools.chain(*tuple))
# printing output
print(out)
|
Method #3 : Using iteration
# Python code to convert list of tuple into list
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
# result list initialization
result = []
for t in tup:
for x in t:
result.append(x)
# printing output
print(result)
|
Method #4 : Using sum
# Python code to convert list of tuple into list
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
# using sum function()
out = list(sum(tup, ()))
# printing output
print(out)
|
Method #5 : Using operator and reduce
# Python code to convert list of tuple into list
import operator
from functools import reduce
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
# printing output
print(list(reduce(operator.concat, tup)))
|
Method #6 : Using lambda
# Python code to convert list of tuple into list
# List of tuple initialization
tup = [(1, 2), (3, 4), (5, 6)]
# Using map for 0 index
b = map(lambda x: x[0], tup)
# Using map for 1 index
c = map(lambda x: x[1], tup)
# converting to list
b = list(b)
c = list(c)
# Combining output
out = b + c
# printing output
print(out)
|