Python: Native Data Structure - Tuples and Lists

In this series of learning "Python for Data Science", we are going to learn various native data structures in Python. These data structures are:

  • Lists and Tuples
  • Sets
  • Dictionaries

Tuples in Python

Tuples are one of the sequence type available in Python. In this list of objects separated by "," (commas) are stored within parentheses - "(" and ")". Tuples stores elements in a ordered sequence and a tuples can not be changed.

When we want to store product prices, we can create a tuples "product_price" which stores product prices in a sequence. A tuples can store integer, float or another tuples as its element.

product_price = (12,33,25,18,45,39)

Main difference between lists and tuples is that tuples are "immutable" but lists are "mutable". Immutable means it can not be changed. If we want to change a tuple, we can create a new tuple.

Some of the common operations on tuples are:

Indexing

If we were to select element of a tuple, we can use the index as elements are stored in a sequence from zero onward. Indexing is done using squared bracket "["/"]".

product_price[3]
18

Slicing

For accessing single elements, we can use index, we can select multiple elements using range of indexes. It gives output as another tuple.

product_price[3:5]
(18, 45)

Combining Tuples or Adding sequences

We can combine multiple tuples and create another tuple.

prices_1 = (12,42,56.0, 33.75, 18.15)
prices_2 = (33, 45.23, 77.56, 26.55, 22)
prices_1+prices_2
(12, 42, 56.0, 33.75, 18.15, 33, 45.23, 77.56, 26.55, 22)

Operations on Tuple Elements

We can multiple with a number n and it will create another tuple with repeating original tuple with n times.

p =(12,13)<br>
p1 = p*4<br>
p1
(12, 13, 12, 13, 12, 13, 12, 13)

One other point is that in the tuple, element could be of different data types.

names =('Ram','John',11,'Michelle')
names
('Ram', 'John', 11, 'Michelle')

Lists in Python

Similar to tuple, a list is also a sequence of ordered elements. But unlike a tuple, a list is mutable (element/s can be changed).

A list can be created using squared bracket.

product = ['Tomato','Potato','Banana','Apple']
product
['Tomato', 'Potato', 'Banana', 'Apple']

Creating a list

A list can be created as an empty list, assign a list of elements directly or using an existing list to create a new one.

# create an empty list
product_list=[]

# create a list of 5 colors
colors =['Red','White','Green','Blue','Black']
# Combine two lists
colors1 = ['Orange','Yellow']
colors+colors1

['Red', 'White', 'Green', 'Blue', 'Black', 'Orange', 'Yellow']

# slice an existing list
colors[2:4]
['Green', 'Blue']

Accessing List

We can access element/s of a list using index or range of indexes.

product = ['Tomato','Potato','Banana','Apple','Orange', 'Guava','Berry']
# Accessing first element
product[0]
'Potato'
# Accessing multiple elments
product[1:3]
['Potato', 'Banana']

Appending a list

We can add elements to an existing list (it can be changed/mutable). It can be done using append function.

colors =['Red','White','Green','Blue','Black']
colors.append('Orange')
colors
['Red', 'White', 'Green', 'Blue', 'Black', 'Orange']

Loops on lists in Python

Scenario: we have two lists - first_name (first name) and last_name (surname) and we want to combine to create a list of full names.

Steps:

  • Create a list of first names
  • Create a list of last names
  • Define an empty list of full names with required size or length
  • Run a loop for each elements and combine the element values to create a new element which will be assigned to full name list element. We should keep separator - space- between first and last name.
first_name =['Steven','Neena','Lex','Alexander','Bruce','David','Valli','Diana','Nancy','Daniel']
last_name =['King','Kochhar', 'De Haan', 'Hunold','Ernst','Austin','Pataballa','Lorentz','Greenberg','Faviet']
# create empty list
fullname=['' for p in range(0,len(first_name))]
# combine element of first and last name
for i in range(0,len(first_name)):
    fullname[i]=first_name[i]+" "+last_name[i]

# output
['Steven King',
 'Neena Kochhar',
 'Lex De Haan',
 'Alexander Hunold',
 'Bruce Ernst',
 'David Austin',
 'Valli Pataballa',
 'Diana Lorentz',
 'Nancy Greenberg',
 'Daniel Faviet']

Next: Sets in Python (Python for Data Science Series)

Leave a Comment