Analytics Today: Python Tuple Vs Lists

Tuple Vs. List in Python is the most searchable word. Here, I have shared the differences between these two.


Analytics Today: Python Tuple Vs Lists
Analytics Today: Python Tuple Vs Lists

1. Python List

  1. It is simple to create a list in Python. 
  2. Comma-separated elements inside a square bracket [] make a list.
  3. The list can contain any number of elements of different data types (integer, float, character, etc.) 
  4. A list can contain another list. And it is called a nested list


List Example

#Illustration of creating a list 
new_list=[1, 2, 3, 4] 

print(new_list) 
# Homogeneous data elements 
new_list1=[1, "John", 55.5] 

print(new_list1) 
# Heterogeneous data elements 
new_list2=[111, [1, "Clara", 75.5]] 
# Nested list 
print(new_list2)


Output



[1, 2, 3, 4]

[1, ‘John’, 55.5]

[111, [1, ‘Clara’, 75.5]]



2. Python Tuple

  1. It is simple to create a tuple in Python. 
  2. The elements of a list are enclosed in square brackets []. But these are put in parenthesis (), separated with commas. 
  3. A tuple can have any number of elements of heterogeneous type (integer, float, string, list, etc.)


Tuple Example

#Illustration of unpacking a tuple 
 new_tuple2=(111, [1, "Clara", 75.5], (2, "Simon", 80.5)) 

# Nested tuple 
print(new_tuple2) x, y, z=new_tuple2 
print(x) 
print(y) 
print(z) 



Output



111

[1, ‘Clara’, 75.5]

(2, ‘Simon’, 80.5)

Comments

Popular Posts

How to Fix Python Syntax Errors Quickly

7 AWS Interview Questions asked in Infosys, TCS

Python 'getsizeof' Command the Real Purpose

Hyperledger Fabric: 20 Real Interview Questions

How to Decode TLV Quickly

AWS Vs Azure Load Balancers Top Insights

How to Use the ps Command in Linux

QlikView Server vs Publisher Top Differences Really Useful to Your Project

Top 10 SCALA Quiz Questions for Programmers

Python Syntax Errors Cheat Sheet