Greetings. This would be a very short post on an introductory aspect of Python LISTS which I learnt while currently learning the Python programming language. Python language is really cool for beginners because of its simple and easily readable syntax.
What are List(s)?
List(s) also referred to Arrays in some other programming languages, is a python data type that gives us the ability to store large sets of data, whether strings, integers, booleans or even another List. They are also mutable, meaning that you can alter the contents i.e. changing, removing or adding new items to it. List(s) is denoted using a square-bracket [ ] and each item in a list is assigned an index, and is assigned a variable name. A basic example of a List is:
# a simple list of sports done in my school
sports = ['Basketball', 'Table Tennis', 'Soccer'] # assigning a list to a
# variable
print(sports)
Here, the variable "sports" was assigned a list of sports done in my school and returns ['Basketball', 'Table Tennis', 'Soccer'] when the code is ran.
Accessing the elements of a Lists.
To access an item in a list, you make use of its index. Whenever you declare a List variable like the sports we declared, each item is assigned an index(which is stored in the memory). Index in python starts to count from zero and is accessed using square-brackets. Accessing the items of the sports list variable
# accessing the items of the list
print(sports[0]) # returns the item at index 0 which is "Basketball"
print(sports[2]) # returns "Soccer" which is at index 2.
List within a list, list with mixed data types...
Lets say we have a list variable car_companies that stores list of car companies, with a boolean, and another list.
# mixed data types and a list within a list
car_companies = ['Toyota', 'Mazda', True, ['Lexus', 34]]
print(car_companies[1]) # returns 'Mazda' at index 1
print(car_companies[3]) # returns '['Lexus', 34] at index 3, which is a
# list inside of a list
Accessing the items of the list within the list, you use double square-brackets to access them. example
1. car_companies = ['Toyota', 'Mazda', True, ['Lexus', 34]]
2. print(car_companies[3]) # returns '['Lexus', 34]' at index 3, which is
# a list inside of a list
3. print(car_companies [3][0]) # returns 'Lexus'
4. print(car_companies [1][0]) # returns 'M'
What the last two lines of code means: For line 3, go to index 3 of the list variable car_companies, which returns ['Lexus', 34], then inside that item returned, in this case is a list, return the item that is at index of zero, which would return 'Lexus'. So also for line 4, what the code is saying is, go to index 1 of the list variable, which returns a String data type, "Mazda" (and in python, strings are also assigned indexes when they are created), then return the letter at index 0 of the string "Mazda" that was returned, in this case the letter "M".
I believe with this my article of basic knowledge acquired while learning the python language, would help you understand the basic workings of a list and how to instantiate one. There are other amazing things that can be done to a list, like copying the content of a list, slicing e.t.c.
Top comments (0)