Python List and Tuple

Dealing with data in a structured format is quite generous, and this is possible if those data are set accordingly in a specific manner. So, Python provides these data structures named ‘lists’ and ‘tuples’ that are used to organize data in a single set. Python has six built-in sequences, and among them, the most famous is “lists and tuples”.

The lists are containers that hold some other objects in a given order. It usually puts into practice the sequence protocol and allows programmers to add or remove objects from that sequence. Each element of the sequence is assigned a number, i.e., the index and the first index is 0 (zero). This versatile data-type of Python is written in a sequence of the list separated by commas between expressions.

Python List

Python Lists are containers to store a set of values of any data type.

friends = [‘Apple’, ‘Akash’, ‘Rohan’, 7, False]

The list can contain different types of elements such as int, float, string, Boolean, etc. Above list is a collection of different types of elements.

List Indexing

A list can be index just like a string.

L1 = [7, 9, ‘Sandeep’]

L1[0] – 7

L1[1] – 9

L1[70] – Error

L1[0:2] – [7,9]         (This is known as List Slicing)

List Methods

Consider the following list:

L1 = [1, 8, 7, 2, 21, 15]

  1. sort() – updates the list to [1,2,7,8,15,21]
  2. reverse() – updates the list to [15,21,2,7,8,1]
  3. append(8) – adds 8 at the end of the list
  4. insert(3,8) – This will add 8 at 3 index
  5. pop(2) – It will delete the element at index 2 and return its value
  6. remove(21) – It will remove 21 from the last
Tuples in Python:

A tuple is an immutable (can’t change or modified) data type in Python.

a = ()              #It is an example of empty tuple

a = (1,)           #Tuple with only one element needs a comma

a = (1, 7, 2)   #Tuple with more than one element

Once defined, tuple elements can’t be manipulated or altered.

Tuple methods:

Consider the following tuple,

a = (1, 7, 2)

  1. count(1) – It will return the number of times 1 occurs in a.
  2. index(1) – It will return the index of the first occurrence of 1 in a.

Some of the characteristics features of Tuples are:

  • Tuples are defined in the same way as lists.
  • They are enclosed within parenthesis and not within square braces.
  • Elements of the tuple must have a defined order.
  • Negative indices are counted from the end of the tuple, just like lists.
  • Tuple also has the same structure where commas separate the values.

Exercise: Python List & Tuple

  1. Write a program to store seven fruits in a list entered by the user.
  2. Write a program to accept the marks of 6 students and display them in a sorted manner.
  3. Check that a tuple cannot be changed in Python.
  4. Write a program to sum a list with 4 numbers.
  5. Write a program to count the number of zeros in the following tuple:

a = (7, 0, 8, 0, 0, 9)

Was this article helpful?
YesNo

Leave a Comment

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