This article will help in developing the understanding of Python tuples, lists, sets and dictionaries. We will see some examples of their implementations and their use cases for some tasks. The coding part will be done in VS Code. If you have not installed VS Code or want to start from scratch, please visit our previous blogs.
Python tuples, lists, sets and dictionaries – table of contents:
- Introduction to Python
- Lists in Python
- Basic operations with lists
- Python tuples
- Difference between Python tuples and lists
Introduction to Python tuples, lists, sets and dictionaries
In the previous blog, we saw how we can use the variables and data types in python. We also investigated some useful functions related to data types and variables.
Python is a powerful scripting language. It has many built-in data structures available for use. These structures are so powerful in handling the data, yet they are simple to implement.
These basic structures are of four types – list, tuple, dictionary and set.
Lists in Python
Lists are in-built n python. These are mutable, so items can be added or removed from them without altering their original contents, and elements can be accessed through index.
They are so general that they can be used to store any type of object, from strings to numbers, even the objects also. Moreover, it is not required to have all the elements of the same type, A list can have elements of different types.
To use list, you need to initialize a variable by [].
For Example:
# An empty list empty_list = [] # List with same type of elements same_type_list = [‘1’, ‘3’, ‘7’, ‘10’] # List with different types of elements diff_type_list = [‘John’, ‘Dev’, 1.90, True]
Now we know how to initialize the variable with list. Let’s see some basic operations.
Basic operations with lists
Ever wanted to traverse the items in a list without going through them one-by-one? Python provides several useful functions. They allow you to manipulate them without iterating over the list or looping through each item.
The following are Python’s five most used list operations:
1. len(list) – It returns the length of the list. It also helps in iteration when one wants to traverse the list.
For Example:
# Printing the length of the list some_list = ['k', 'u', 'm', 'a', 'r'] print(len(some_list)) # Traversal of list for i in range(len(some_list)): print(some_list[i])
# Output 5 k u m a r
2. max(list) – It returns the item in the given list with the highest value, if there is no tie then it returns an error.
For Example:
# Printing the maximum of the number stored in list num_list = [1, 2, 3, 4, 5, 12, 78, 900, 100] print(max(num_list))
# Output 900
3. min(list) – it returns the item in the given list with lowest value, if there is no tie then it returns an error
For Example:
# Printing the minimum of the number stored in list num_list = [1,2,3,4,5,12,78,900,100] print(min(num_list))
# Output 1
4. sort(list) – This function sorts through all these data and puts them in ascending/descending order by default but if key parameter is passes it sorts the list based on the evaluation of the function on the elements.
Reverse parameter controls whether the sorted(ascending order) list be given as it is sorted, or it gets reversed i.e., in descending order.
The syntax is list.sort(reverse=True|False, key= some function)
For example:
num_list = [1,2,3,4,5,12,78,900,100] print(num_list) num_list.sort() print(num_list) num_list.sort(reverse = True) print(num_list)
Output: [1, 2, 3, 4, 5, 12, 78, 900, 100] [1, 2, 3, 4, 5, 12, 78, 100, 900] [900, 100, 78, 12, 5, 4, 3, 2, 1]
5. map(function, sequence) – This function here applies a function on each element of the list. The syntax is given by map(fun, iter). Here ‘fun’ is the function that is supposed to be applied on every element of ‘iter’.
For Example:
def square(n): return n * n numbers = [1, 2, 3, 4] result = map(square, numbers) print(list(result))
output: [1, 4, 9, 16]
There are so many other functions are there for lists. Now let’s see what tuples are.
Python tuples
They can be created by simply declaring a tuple within parentheses, (), or by converting any sequence into a tuple using the built-in constructor tuple().
# Creating an empty tuple empty_tuple = () seq_set = {1,2,3} seq_list = [2,3,4,5] print(type(seq)) print(type(seq_list)) # Converting set into tuple seq_set_tuple = tuple(seq_set)
Output: <class 'set'> <class 'list'> # Creating an empty tuple empty_tuple = () seq_set = {1, 2, 3} seq_list = [2, 3, 4, 5] print(type(seq_set)) print(type(seq_list)) # Converting set into tuple seq_set_tuple = tuple(seq_set) print(type(seq_set_tuple)) output: <class 'set'> <class 'list'> <class 'tuple'>
Tuples are like lists with the difference that tuples are immutable. Then why do we use the tuples.
Difference between Python tuples and lists
Tuples are immutable while lists are mutable. This means that tuples cannot be changed after they have been created, while lists can be edited to add or remove items.
Like list, a tuple is also a sequence of data elements, which are not necessarily of the same type.
For Example:
# Tuple with same type of elements same_type_list = ('1', '3', '7', '10') print(same_type_list)
Output: ('1', '3', '7', '10')
# List with different types of elements diff_type_list = ('John', 'Dev', 1.90, True) print(diff_type_list)
# Output ('John', 'Dev', 1.9, True)
Next blog post glimpse
We will be learning about sets and dictionaries in the upcoming blogs.
You may also like our JavaScript Course from Beginner to Advanced.
Python Course From Beginner to Advanced in 11 blog posts:
- Introduction to Python Course. Part 1 Python Course from Beginner to Advanced in 11 blog posts
- Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts
- Python tuples, lists, sets and dictionaries. Part 3 Python Course from Beginner to Advanced in 11 blog posts
- Python sets and dictionaries. Part 4 Python Course from Beginner to Advanced in 11 blog posts
- Conditional statements in Python. Part 5 Python Course from Beginner to Advanced in 11 blog posts
- Loops in Python. Part 6 Python Course from Beginner to Advanced in 11 blog posts
- Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts
- Advanced functions in Python. Part 8 Python Course from Beginner to Advanced in 11 blog posts
- Python classes and objects. Part 9 Python Course from Beginner to Advanced in 11 blog posts
- Files in Python. Part 10 Python Course from Beginner to Advanced in 11 blog posts
- Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts