2.1.1
Lists
Lists
Lists
Lists are a data structure quite similar to an array.
![Illustrative background for Lists](https://image-v2.cdn.app.senecalearning.com/2018-09/3fba2487-0107-4ee7-9e33-50edfe5b6f04/checklist-ticklist-tick-list-,h_400,q_80,w_640.jpg)
![Illustrative background for Lists ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/3fba2487-0107-4ee7-9e33-50edfe5b6f04/checklist-ticklist-tick-list-,h_400,q_80,w_640.jpg)
Lists
Lists
- Lists store multiple elements of different data types under the same identifier.
- There can be a variable number of elements - they never get full.
![Illustrative background for Lists in pseudocode](https://image-v2.cdn.app.senecalearning.com/2018-09/3d7d187f-ce76-4b7e-8fa7-8aabc6b43a25/to-do-list,h_400,q_80,w_640.jpeg)
![Illustrative background for Lists in pseudocode ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/3d7d187f-ce76-4b7e-8fa7-8aabc6b43a25/to-do-list,h_400,q_80,w_640.jpeg)
Lists in pseudocode
Lists in pseudocode
- A list can be declared as follows:
- pupils = []
- Values can be added to the end of the list as follows:
- pupils.append("Alice")
- Values can be accessed as follows:
- print("The first student is " + pupils[0])
2D Lists
2D Lists
A 2D list is the data structure which consists of a list of lists.
![Illustrative background for 2D List](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/1.1.3 - Periodic table/1000px-Periodic_table_by_Mendeleev,_1871.svg-min,h_400,q_80,w_640.jpg)
![Illustrative background for 2D List ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/1.1.3 - Periodic table/1000px-Periodic_table_by_Mendeleev,_1871.svg-min,h_400,q_80,w_640.jpg)
2D List
2D List
- A 2D list is a data structure where lists are filled with other lists.
- This can be used to represent a 2D surface, or records.
![Illustrative background for Accessing elements](https://image-v2.cdn.app.senecalearning.com/2018-09/f030eba3-b68d-4b3c-a597-26e9d34b9eef/today-list,h_400,q_80,w_640.jpeg)
![Illustrative background for Accessing elements ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/f030eba3-b68d-4b3c-a597-26e9d34b9eef/today-list,h_400,q_80,w_640.jpeg)
Accessing elements
Accessing elements
- To access an individual list we can access the list as usual:
- list = my2DList[4]
- To access an individual element, we use this syntax:
- list[5][0]
- This would get the first element ([0]), of the sixth list ([5]).
![Illustrative background for Uses of 2D lists](https://image-v2.cdn.app.senecalearning.com/2018-09/a318dc7e-cd70-42c7-a544-f41b36c8ef18/coup,h_400,q_80,w_640.jpg)
![Illustrative background for Uses of 2D lists ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/a318dc7e-cd70-42c7-a544-f41b36c8ef18/coup,h_400,q_80,w_640.jpg)
Uses of 2D lists
Uses of 2D lists
- 2D lists are often used to represent a 2D surface, for example a chess board:
- Each element of the 2D list could contain the chess piece in that location.
- 2D lists can be used to represent a database:
- Each inner list can store a record of the database.
- The outer list can store each record.
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
Jump to other topics
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered