All elements in a Python list must have the same data type

3 Advanced Operations in Python Lists with Examples

Merging, List Comprehension and Append Elements

Rukshan Pramoditha

Aug 28·5 min read

Photo by Chris Lawton on Unsplash

The list is one of the built-in data types in Python. It is used to store multiple Python objects [integer, float, string, etc.] in a single variable. The elements are placed inside a pair of square brackets.

list_1 = [1, 0, 10] # A list of integers
list_2 = [5.3, 10.0, 7.5] # A list of floats
list_3 = ['A', 'I', 'Help', 'Thanks'] # A list of strings
list_4 = [1, 5.3, 'A'] # A list of mixed Python objects!

Some interesting facts on Python lists

  • The individual elements in a list can be selected by using zero-based indexing [zero for the first element].
  • Lists are mutable. We can add or remove items or change the value of an element in a list after we create it.
  • Elements of different data types can be included in the same list.
  • The list[] constructor can be used to create a new list.
  • A list of subsequent integers can be created by using the range[] function inside the list[] constructor.

Some advanced operations in Python lists

There are many operations in Python lists. Here, more emphasis will be given to Merging, List Comprehension and Append Elements.

1. Merging lists with the zip[] function

The zip[] function merges two lists into a new list by taking corresponding elements from the two lists. The result is a zip object. We can place that object inside the list[] constructor to reveal the new list of tuples.

list_1 = [1, 2, 3]
list_2 = ['one', 'two', 'tree']
list[zip[list_1, list_2]]
[Image by author]

We can also merge two lists with different lengths. In this case, the new list is created in favor of the shortest list.

list_1 = [1, 2, 3, 4, 5] # 5 elements
list_2 = ['one', 'two', 'tree'] # Only 3 elements
list[zip[list_1, list_2]]
[Image by author]

2. List Comprehension

List comprehension is a one-line code technique used to build a new list by iterating the elements inside a list. The new list is created in the way that the original list elements satisfy a certain expression or condition or both.

The syntax is:

[ for in ]

Imagine that we have the following list.

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

Now, we want to create a new list that contains the square values of all the elements in the above list. For this, we can use list comprehension.

[x**2 for x in new_list]
[Image by author]

List comprehension can also contain a condition. In that case, the syntax is:

[ for in if ]

Now, we want to create a new list that contains the square values of selected elements [e.g. odd numbers] in the original list. For this, we can use list comprehension with a condition.

[x**2 for x in new_list if x % 2 == 1]
[Image by author]

3. Append Elements

The previous two operations did not modify the original lists on which the operations were done. But, append elements is an in-place operation that modifies the original list directly. This operation adds elements to the end of the list. This can be done by using the append[] method of the list object.

Usually, we create an empty list and then append elements as we need. The above square-value problem can also be solved by using this approach.

a = [] # Empty listfor i in new_list:
sq = i**2
a.append[sq]
a
[Image by author]

At the creation, a is an empty list. Now, a has been modified and contains 5 elements meaning that the append operation was done in place.

The append operation is very useful when you want to store the calculated values for plotting at a later time. For example,

x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
for i in x:
ex = i**3
y.append[ex]
import matplotlib.pyplot as plt
plt.plot[x, y]
[Image by author]

The coordinates can be seen by using the merge operation!

coordinates = list[zip[x, y]]
coordinates
[Image by author]

Conclusion

Python lists are very useful because we can perform some advanced operations with them using just one or two lines of code. A great alternative to Python lists is NumPy arrays, especially when we consider the performance. When executing, NumPy arrays are significantly faster than Python lists. However, we can easily create NumPy arrays using Python lists or convert lists into arrays. Python list is a built-in data type. It is closely associated with other data types and libraries in Python.

My readers can sign up for a membership through the following link to get full access to every story I write and I will receive a portion of your membership fee.

Sign-up link: //rukshanpramoditha.medium.com/membership

Thank you so much for your continuous support! See you in the next story. Happy learning to everyone!

Special credit goes to Chris Lawton on Unsplash, who provides me with a nice cover image for this post.

Rukshan Pramoditha
20210828

Video liên quan

Chủ Đề