Working with Python Data Structures-Part 1

Python Data Structures help you organize data and use it in the applications you intend to build using Python language.

Python provides the following data structures:

  • List
  • Tuples
  • Dictionaries
  • Sets

We will review each in more detail and also try to see what could be the practical applications for each of these data structures.

Before we look at the different data structures, we will look at the concept of Strings.

Strings:

  • A String is an immutable data type (means it cannot be changed)
  • It is a sequence of Unicode characters wrapped inside single, double, or triple quotes.
  • Examples of strings are ‘Today is Saturday’, “Today is a Holiday”, “” Tomorrow is Sunday”””
  • Use print command to print a string
    • e.g. string1= ‘ Today is Saturday’
    • print(string1)
    • Output- ‘ Today is Saturday’
  • Use length function to determine the length of the string as shown in below screenshot
  • Strings support indexing- which means you can retrieve a certain character from the string
  • Use below syntax:
    • IF Day=’Saturday’, then Day[1]= a- note that index starts at 0 and 1st character of this string can be retrieved as Day[0]= S
    • Note: use print statement to print the results
  • Note that Python supports negative indexing. To retrieve the last character of a string, use -1
  • Using the above example, Day[-1]=y and Day[-2]=a

In this post, we will look at the concept of ‘Lists’ in Python in more details

Lists in Python- A Basic Understanding:

  • A list is a collection of comma separated values written within square brackets
  • Example of a list in python:
    • even_numbers_greater_than_zero=[2,4,6,8,10]
  • A few of the important characteristics of Lists are as below:
    • Lists may contain items of same or different types (e.g. int, float, string etc.).
    • homogeneous_list= [2,7,9,11,13]
    • heterogenous_list=[“yes”, 1, “True”]- note that the list has different types- integers and strings in this case
    • Lists can be indexed
      • even_numbers_greater_than_zero=[2,4,6,8,10]
      • even_numbers_greater_than_zero[2]=6 #indexing returns the required item
    • Lists can be sliced
      • even_numbers_greater_than_zero[-3:]= [6,8,10] #slicing returns last 3 items of the list to create a new list
    • Lists can support concatenation
      • even_numbers_greater_than_zero=[2,4,6,8,10]
      • even_numbers_greater_than_zero_additional= [12,14,16,18]
      • print (even_numbers_greater_than_zero+even_numbers_greater_than_zero_additional)=[2,4,6,8,10,12,14,16,18]- creates a new list which contains a new list with items from both of the above lists
    • Lists are mutable- which means it is possible to change its content
      • even_numbers_greater_than_zero=[2,4,6,8,10]
      • even_numbers_greater_than_zero[2]= 7
      • even_numbers_greater_than_zero=[2,4,7,8,10] #the 3rd element in this list is modified based on the above statement
    • Lists support len function
      • While using the len function with lists, it lists down the number of items in the list
      • len(even_numbers_greater_than_zero)= 5
    • It is possible to create nested lists
      • even_numbers_greater_than_zero=[2,4,6,8,10]
      • odd_numbers_greater_than_zero=[1,3,5,7,9]
      • numbers= [even_numbers_greater_than_zero, odd_numbers_greater_than_zero]
      • print(numbers)=[[2,4,6,8,10], [1,3,5,7,9]] #Python created a list of nested lists

Lists in Python- When to use?

  • Use a list when you want to store different kinds of data type that may need to be changed in future
  • Use a list when you want to manipulate the data using slicing, concatenating, updating, deleting items in the list.

The above is my basic understanding of Lists as I teach myself the concept of ‘Lists’ as a part of Python data structures.

In subsequent posts, I will try to cover my understanding of other data structures in Python.

References:

I am using the official Python documentation for learning Python

Below is the link for reference (I am working on Python 3.9.6 version)

https://docs.python.org/3.9/

For Python Setup, refer to my below posts:

Leave a Reply

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