Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming /Difference Between List and Tuple

Difference Between List and Tuple

February 15, 2018Posted byLithmee

Key Difference – Listvs Tuple

Pythonis a general-purposehigh-level programming language.很容易阅读和学习。因此,这是一个common language for beginners to start computer programming. Python programs are easy to test and debug. It is a language used to build a variety of applications. Some of them are machine learning, computer vision, web development, network programming. Python is used for building algorithms for solving complex problems. Two data storing methods of Python are List and Tuple. The elements of a list can be changed. So, a list is mutable. The elements of a tuple cannot be changed. So, a tuple is immutable. This article discusses the difference between list and tuple. Thekey differencebetween list and tuple is thata list is mutable while a tuple is immutable.

CONTENTS

1.Overview and Key Difference
2.What is List
3.What is Tuple
4.Similarities Between List and Tuple
5.Side by Side Comparison – List vs Tuple in Tabular Form
6.Summary

What is List?

In programming languages such asC or C++,arrayis used to hold the elements of the same data type. But in Python List, all elements need not be of same time. Each item in the list is separated by a comma. All elements are included inside square brackets. An example of a list is list1 = [1, “abc”, 4.5]; The index of a list starts with zero. Therefore, the element 1 has the index 0, and abc has the index 1 etc. It is also possible to use the negative index. The last element of the list has the index -1. Then the element “abc” has the index of -2 etc.

It is possible to take a sequence of elements from the list. This is calledslicing.When there is a list as follows, which is list1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’], the statement print(list1[2:5]) will print c,d,e. The element in index two is included but not the element in index five.

Lists are mutable. Therefore, the elements in the list can be changed. Assume that there is a list as, list1 = [2,4,6,8]. If the programmer wants to change the first element to value 1, then he can change it by writing the statement list1[0] = 1. Python language has already inbuilt functions to add new items to a list. It is the append function. When there is a list such as list1= [1,2,3], the programmer can add the new element 4 using list1.append(4).

The elements of a list can be deleted using the del () by passing the relevant index. Assume that there is a list as list1= [1,2,3,4]. The statement del(list1[2]) will give 1,2, 4. The element in the second index is 3. That element will be deleted. When there is are two lists as list1= [1,2,3] and list2 = [4,5,6], the programmer can join these two lists using concatenation operation as list1+list2. It will give a combined list [1,2,3,4,5,6].

There is a number of list methods available to handle list operations. Some of them are insert (), remove (), count () etc. Implementing a list in Python is easy when compared to the arrays in other programming languages such as C, C++ etc.

What is Tuple?

A tuple is similar to a list. Each item in the list is separated by a comma. All elements are included in parenthesis. A tuple can have a different type of elements. Each element is separated by a comma. An example of tuple is tuple1 = (1,2,3). The first element has the index 0. The second element has the index 1 and so on. Tuple can also have negativeindexing.So, the value 3 has the index -1. Value 2 gas the index -2 and so on.

The programmer can take a sequence of elements in the tuple. Assume that there is tuple, tuple1= (1,2,3,4,5). The statement print(list1[2:5]) will print 3,4. The element in index two is included but not the element in index five.

Tuples are immutable. Therefore, the elements in the list cannot be changed. Changing the elements will give errors. But if the element is a mutable data type, then its nested items can be changed. Assume that there is a tuple as tuple1 = (1,2, [3,4]). Even this is a tuple, the element in index 2 has a list. To change the 1stelement in that list to 5, the statement tuple1[2][0] = 5 can be used. As the tuple is immutable, the elements cannot be deleted. But using the del function, the entire tuple can be deleted. e.g. del (tuple1).

Difference Between List and Tuple

Figure 01: Examples of a List and a Tuple

There are functions provided by Python for tuple-based operations. The len () function helps to find the number of elements in a tuple. The max and min functions can be used to find the maximum value and the minimum value of the tuple. Implementing a tuple is an easy process comparing with arrays in another programming language such as C/ C++.

What are the Similarities Between List and Tuple?

  • Both List and Tuple are used to store a set of elements in Python.
  • The index of both list and tuple starts with zero.
  • Each element is separated by a comma in both List and Tuple.
  • Both List and Tuple can have different types of elements.
  • The list can contain nested list and tuple can contain nested tuple.
  • Both List and Tuple support negative indexing.

What is the Difference Between List and Tuple?

List vs Tuple

A list is a compound data type in Python programming language which can store different type of data and can change elements once created. A tuple is a compound data type in Python programming language which can store different type of data and cannot change elements once created.
Mutability
A list is mutable. It can be changed once created. A tuple is immutable. It cannot be changed once created.
Enclosing Elements
The elements of a list are enclosed in square brackets. The elements of a tuple are enclosed in parenthesis.
Speed
Iterating through elements in a list is not fast as in a tuple. Iterating through elements in a tuple is faster than list.

Summary – Listvs Tuple

Python uses List and Tuple to store data. The List and tuple can use to store different type of data elements. This article discussed the difference between List and Tuple. The elements in a list can be changed. So, a list is mutable. The elements in a tuple cannot be changed. So, a tuple is immutable. The difference between list and tuple is that a list is mutable while a tuple is immutable.

Download the PDF of List vs Tuple

You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here:Difference Between List and Tuple

Reference

1.tutorialspoint.com. “Python Lists.”The Point,Available here
2.tutorialspoint.com. “Python Tuples.”The Point.Available here

Related posts:

Difference Between Arrays and Linked Lists Difference Between Debugger and Compiler Difference Between Java and Spring Difference Between Open Source and Proprietary SoftwareDifference Between Open Source and Proprietary Software Difference Between Machine Language and Assembly LanguageDifference Between Machine Language and Assembly Language

Filed Under:ProgrammingTagged With:Compare List and Tuple,List,List and Tuple Differences,List and Tuple Similarities,List Definition,List Elements,List Mutability,List Speed,List vs Tuple,Tuple,Tuple Definition,Tuple Elements,Tuple Mutability,Tuple Speed

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Comments

  1. Naresh Reddipallisays

    November 3, 2019 at 12:02 am

    The programmer can take a sequence of elements in the tuple. Assume that there is tuple, tuple1= (1,2,3,4,5). The statement print(list1[2:5]) will print 3,4. The element in index two is included but not the element in index five. –
    *** In this case result should be 3,4,5

    Reply

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Situational and Dramatic Irony

Difference Between Tethered Jailbreak and Untethered Jailbreak

Difference Between Android 2.3 Gingerbread and Android Ice Cream

Difference Between Plastoquinone and Plastocyanin

Difference Between Plastoquinone and Plastocyanin

Difference Between Noble Gas Configuration and Electron Configuration

Difference Between Noble Gas Configuration and Electron Configuration

Latest Posts

  • What is the Difference Between Functional and Performance Ingredients
  • What is the Difference Between Intracerebral Hemorrhage and Subarachnoid Hemorrhage
  • What is the Difference Between Behentrimonium Chloride and Behentrimonium Methosulfate
  • What is the Difference Between Systolic and Diastolic Dysfunction
  • What is the Difference Between Care and Concern
  • What is the Difference Between Ehlers Danlos and Marfan Syndrome
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between.All rights reserved.Terms of Useand Privacy Policy:Legal.