Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 7, 2021 06:31 am GMT

Learning Python-Basic course: Day 11, Multidimensional lists and Tuples

Welcome! Today we will learn Multidimensional lists and Tuples

Multidimensional lists

Multidimensional lists or fondly called list of lists can be made using the following syntax

list=[[1,2,3],[2,3,4],[3,4,5]]

This creates a 3 by 3 list. However, in the memory, the values are stored sequentially.
Let us now see a sample to get things clear

>>> list=[[1,2,3],[2,3,4],[3,4,5]]>>> print(list[1])[2, 3, 4]>>> print(len(list))3>>> print(list[2][1])4>>> print(len(list[0]))3>>> print(list)[[1, 2, 3], [2, 3, 4], [3, 4, 5]]>>> for i in range(0,len(list)):...  for j in range(0,len(list[i])):...   print(list[i][j])...123234345>>> for i in list:...  for c in i:...   print(c,end="")...  print()...123234345

The last statement is the mysterious way python implements it's syntax. Rather than writing in range, we can easily take this shortcut.

We can create multidimensional lists dynamically. Here is a sample showing how to.

a=[]r=int(input("Please enter the number of rows "))c=int(input("Please enter the number of columns "))for i in range(r):    #Append an empty sublist    a.append([])    for j in range(c):        num=int(input("Enter a value "))        a[i].append(num)print(a)for i in a:     for j in i:         print(j, end="")         #here, "" stands for tab, i.e. leave spaces     print()

Output-

Please enter the number of rows 3Please enter the number of columns 2Enter a value 1Enter a value 2Enter a value 3Enter a value 4Enter a value 5Enter a value 6[[1, 2], [3, 4], [5, 6]]1   2   3   4   5   6   

Tuples in Python

Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable.

In other words the basic difference between list and tuple is that list is mutable, while tuple is not. No one can't modify tuple later. Tuple is a predefined (fixed) list.
In similar way, we can make tuples of tuples or tuples of lists too!

Here is a sample

>>> a=('Aatmaj','Zephyr',163,'1234')>>> b=(1,2,3,4,5)>>> c='a','b','c','d'>>> print(a)('Aatmaj', 'Zephyr', 163, '1234')>>> print(len(a))4>>> print(a[3])1234>>> d=((1,2,3),a,b,('a','c','2',5))>>> print(d)((1, 2, 3), ('Aatmaj', 'Zephyr', 163, '1234'), (1, 2, 3, 4, 5), ('a', 'c', '2', 5))>>> print(len(d))4>>> print(len(d[1]))4>>> print(d[2][3])4>>> print(d[4][2])Traceback (most recent call last):  File "<stdin>", line 1, in <module>IndexError: tuple index out of range>>> a.append(3)Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'tuple' object has no attribute 'append'

Exercise-
Let a be a matrix as shown-

   1 2 3a= 4 5 6    7 8 9

Write code for the following output-
1) Print a

2) Print the transpose of a-

147258369

3) Print lower diagonal elements of a

145789

4) Print the top left diagonal

123457

So friends that's all for now. Hope you all are having fun. Please let me know in the comment section below . And don't forget to like the post if you did. I am open to any suggestions or doubts. Just post in the comments below or gmail me.
Thank you all

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too


Original Link: https://dev.to/aatmaj/learning-python-basic-course-day-11-multidimensional-lists-and-tuples-3bfl

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To