A list is a collection used to store data, which is ordered and changeable.
As opposed to arrays, the lists do not need declaration in Python, because they are a part of Python’s syntax.
How to create a list?
Create a List:
friends = ["Mike", "John", "Doe"] print(friends) #result ['Mike', 'John', 'Doe']
Access items from a list
You access the list items by referring to the index number:
friends = ["Mike", "John", "Doe"] print(friends) #result John
How to loop through a list?
You can loop through the list items by using a for loop:
friends = ["Mike", "John", "Doe"] for name in friends: print(name) #Result Mike John Doe
The important characteristics of Python list
The important characteristics of Python lists are:
Lists are mutable
This one of the most important features for lists. Once a list has been created, elements can be added, deleted, shifted or moved.
Everything in Python is an object and all objects in Python can be either mutable or immutable.
Since everything in Python is an Object, every variable holds an object instance.
A mutable object can be changed after it was created while an immutable object can’t.
Objects of built-in types like (int, float, bool, strings, tuple) are immutable.
Objects of built-in types like (list, set, dictionary) are mutable.
Lists can contain any arbitrary objects.
List elements can be accessed by index.
Lists Are Ordered.
You can sort a list, Ascending or Descending, using the sort() method.
The syntax of the sort() method is:
numbers = [7,11,3,5,0,33] numbers.sort(reverse=True) print(numbers) #Result [33, 11, 7, 5, 3, 0]
Alternatively, you can also use Python’s in-built function sorted().
sorted(list, key=…, reverse=…)
The difference between sort() and sorted() is: sort() doesn’t return any value while, sorted() returns an iterable list.
Lists can be nested
In Python any table can be represented as a list of lists (a list, where each element is another list).
For example, below is a table with two rows and three columns:
number_grid = [ [1,2,3], [4,5,6], ] print(number_grid) #Result 6
How to loop through a list?
You can loop through the nested list items by using a double for loop:
number_grid = [ [1,2,3], [4,5,6], ] for row in number_grid: for col in row: print(col)
Python list operations, methods & functions
Which are the differences/similarities between Arrays and Lists?
- Both arrays and lists can be used to store elements, both are mutable, both can be indexed, sliced and iterated through.
- Arrays must be declared, while the lists do not need to be declared.
- Arrays can only be used for specific types, whereas lists can be used for any object.
- The mostly difference between the lists and arrays are the functions you can perform on them.
When it is better to use lists and when to use arrays?
Even if in Python lists are more often used than arrays, there are cases when sometimes it is good to use lists and sometimes to use arrays.
- If you want to do numerical operations on your data, your best choice is arrays, Because, if you try to do mathematical operations on individual elements of a list you will get an error:
- A list is an efficient way of storing data because You can create a list of anything, with any data type; but if you want to store a large amount of data, use arrays because they can store data very efficiently
- Lists use more memory than arrays is because python will allocate a few extra elements when all allocated elements get used. This means that appending items to lists is faster. So if you plan on adding items, a list is the way to go.
- It’s faster to access individual list items in arrays: O(1) versus O(n)
Which are the differences/similarities between Lists and Tuples?
Tuples are used to collect an immutable ordered list of elements, therefore:
- You can’t add elements to a tuple. Tuples don’t have append() or extend() methods.
- You can’t remove elements from a tuple. Tuples don’t have remove() or pop() methods.
but You can still:
- find elements, since this doesn’t change the elements.
- use the “in” operator to check if an element exists in the tuple.
Also are cases when its better use tuples instead of lists, like:
- Tuple’s size is fixed, so it can be stored more compactly than lists which need to over-allocate to make append() operations efficient.
- Tuples refer directly to their elements, therefore this gives tuples a speed advantage for indexed lookups.
- If you’re defining a constant set of values and all you need is to iterate through it, use a tuple instead of a list. It will be faster than working with a list.
List comprehensions provide a short, powerful and elegant way to create lists in Python without having to use different for loops to append values one by one.
List comprehensions in Python are constructed as follows:
list_variable = [x for x in iterable]
Conditionals in List Comprehension
List comprehensions can utilize conditional statement to modify an existing list:
List comprehensions examples:
X,Y,Z and representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum i+j+k is not equal to N.
Solving using list comprehension
x = int(input()) y = int(input()) z = int(input()) n = int(input()) list = [[i, j, k] for i in range(x + 1) for j in range(y + 1) for k in range(z + 1) if i+j+k !=n] print(list) #Resut 2 2 2 2 [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 2], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 2], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]
Hello there!I hope you find this post useful!
I'm Mihai, an online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!