Lists in Python

List Python Data Structures are collections of objects and are mutable, have no fixed size. List is a sequence which is a Python’s built-in data type. Lists are quite useful as these can be used for holding a lot of information together as single entity. Moreover lists can also hold different types of objects in it.

x = [1, 2, 5, 7, 19] # List of integers
y = [1, 3, "Hello", "ComputerScienceHub"] # List containing different object types

How to create a list in Python?

Lists in Python can be created through three approaches.

Accessing Elements inside a Python List

Simplest way to access elements inside a list in Python is use index number inside square brackets after name of list. For example => If there’s a list x = [3, 4, 5] then x[2] will return 5.
Also if number inside square brackets is positive then Python looks from left to right in list(Starting with 0), but in case if number is negative then Python looks from right to left.
So for x = [3, 4, 5] x[1] = 4 and x[-1] = 5

Finding length of a list in Python?

For finding length of a list you can use len() function (This function can also be used for finding string length).
For example – For finding length of a python list x = [29, 12, 35] you just need to do len(x) and this will return length of list(3 in this case).

Nested Python Lists

In Python Programming Language, you can easily nest other types of objects inside Lists. For example – [37, 39, 28] and [[12, 3], [34, 56]] are both valid Python lists.

Conclusion

There’s a lot more to lists in Python Programming Language. I’ll not mess this article with that information here as I want to just provide brief overview of Python Lists here. Also if your interested then you can check out List Methods in Python.

List Methods in Python Programming Language
append()
clear()
copy()
count()
extend()
index()
insert()
pop()
remove()
reverse()
sort()

Moreover If your learning Python Programming Language, then if you want to check that out please go here – Computer Science Hub Python Programming.

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

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

Recent Posts