Tuples in Python | Lists In Python

In this article we will examine the operations that can be performed mainly on tuples, strings and lists. We will look at accessing tuple elements, counting the occurrence of a specific element within the tuples, searching for elements, and joining tuples and an introduction to lists.

Tuples in Python

Tuples in Python are the same lists with one exception. Tuples are immutable data structures. Just like lists, they can consist of elements of different types, each individual element is separated by commas. Tuples are enclosed in parentheses, not square brackets. From a tuple, you can extract elements and take slices. However, you cannot change its elements. The tuple data type type has no methods for adding and removing elements but allows duplicate values.

#Program to show Tuple demonstration
tuplex = ("Red", "Yellow", "Green", "Red", "Blue")
print(tuplex)

#To access specific individual tuple elements, we mention the tuple index. Index always starts from 0.
print(tuplex[0])   # To access  'Red' element of tuple 'tuplex'
print(tuplex[4])   # To access  'Blue' element of tuple 'tuplex'

#To access/splice specific range of tuple elements we specify the starting element index, a colon : and the ending element
print(tuplex[1:3]) # To access  'Red', 'Yellow' and 'Green' element of tuple 'tuplex'
Output:
('Red', 'Yellow', 'Green', 'Red', 'Blue')
Red
Blue
('Yellow', 'Green')

To delete the entire tuple we can use the del command followed by the tuple name. To concatenate two or more tuples together we use the tuple name followed by the ‘+’ operator, followed by the tuple name to which we want to combine.

Syntax:
//Concatenation 

tuplename3 = tuplename2 + tuplename1

//Tuple Deletion
del tuplename3

We can utilize the count() function to return the number of times an element occurs within a tuple, and index() function to search for the index of a specified element.

Syntax:
//Tuple Count function
tuplename.count("value")

//Tuple Index function
tuplename.index("value")

Let us look at a program involving these operations

#Program to demonstrate various tuple operations
tuple1 = ("x", "y" , "z")
tuple2 = ("a", "b", "c", "y")

#Concatenation of tuple1 and tuple2
tuple3 = tuple1 + tuple2
print(tuple3)

#Count of element occurrences within a tuple
x = tuple3.count("y")
print(x)
#Indexing of specified tuple element
x = tuple3.index("y")
print(x)
Output:
('x', 'y', 'z', 'a', 'b', 'c', 'y')
2
1
Traceback (most recent call last):
  File "main.py", line 17, in <module>
    print(tuple3)
NameError: name 'tuple3' is not defined

Lists in Python

A list is a very handy data structure that can store different types of data. It can also be called a data sequence.  Lists have an index order starting from 0 for first position of element. This means that every item in the list has an index that will not change unless you manually change it. However, unlike tuples lists support mutability which means you can add or remove items from them.

The syntax rules specific to certain data types must be followed within the list. So, if the value is a string it must have quotes, then they must be used inside the list, and for numbers and Boolean values ​​they do not need to be used. Lists in Python simulates to array in other languages like C++.

They are used for a wide variety of purposes and support various operations:

.append ()method for adding items to the list
.insert ()to add items to a specific location in the list
.index ()to get the index of an element
.clear ()to clear the list
.remove ()to remove a list item
.reverse ()to expand the list in reverse order
.count ()to count the number of items in a list
sum ()to add list items
min ()shows the element with the lowest value in the list
max ()the element with the highest value in the list

In the next article we will explore these list operations and functions in detail along with various string operations.

Leave a Comment