Open In App

Appending Item to Lists of list using List Comprehension | Python

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

If you are a Python user, you would know that in Python, we can use the append() method to add an item to an existing list. This list may already contain other items or be empty. Further, the item to be added can simply be a number a character, or even an entire tuple or list. However, if you are trying to append an item to lists within a list comprehension, then you might be having a hard time.

Although list comprehensions are quite useful since they help in creating new sequence data types quickly and shortly without making use of for loops, using the append() function within a list comprehension isn’t the same as it is normally. But worry not because in this article, we are going to discuss all appending items to lists within a list comprehension in Python.

Append an Item to Lists Within A List Comprehension in Python

Below are some of the ways by which we can append an item to lists within a list comprehension in Python:

  • Using the ‘Or’ operator
  • Using a non-in-place operator like ‘+’
  • Using List Comprehension without the assignment operator
  • Using the Extend Method

Append an Item to Lists Within A List Using or Operator

To resolve the problem of getting ‘None’ values in the output, you can make use of the ‘or’ operator inside the list comprehension. You can see that this time the code runs as desired. This is because this time when the append() function returns ‘None’, which is a ‘False’ value, the ‘or’ operator further looks for a ‘True’ value to operate it with. And naturally, the ‘True’ value found is nothing but ‘x’(with an updated item) itself.

Python3




#list containing lists
squares = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]
  
#use list comprehension to append an item
squares = [x.append('10') or x for x in squares]
  
#print updated list
print(squares)


Output

[[1, 1, '10'], [2, 4, '10'], [3, 9, '10'], [4, 16, '10'], [5, 25, '10']]


Using a Non In-Place Append Operator

By now, you know that the append() function works by updating list items in-place. However, to work in a list comprehension, we need an operator that does not update values in-place. Thus, instead of using append() to add item to lists within a list comprehension, you can make use of the ‘+’ operator as follows:

Python3




#list containing lists
squares = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]
  
#use list comprehension to append an item
squares = [x + [10] or x for x in squares]
  
#print updated list
print(squares)


Output

[[1, 1, 10], [2, 4, 10], [3, 9, 10], [4, 16, 10], [5, 25, 10]]


Python Append List to a List Without the Assignment Operator

List comprehensions are meant to create new lists and not update the existing ones. This was the underlying reason why we were getting ‘None’ values with the in-place append() function. However, if we drop the assignment operator within a list comprehension, then we would be actually be getting the list comprehension to update the original list. This is nothing but the use of side effects of list comprehension which is not recommended. But still, here is the code for your reference:

Python3




#list containing lists
squares = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]
  
#use the append method inside list comprehension 
[square.append(10) for square in squares]
  
#print updated list
print(squares)


Output

[[1, 1, 10], [2, 4, 10], [3, 9, 10], [4, 16, 10], [5, 25, 10]]


Python Append List to a List Using Extend() Method

The Extend() method in Python works just like the append() method, except that it can add multiple elements to an iterable object instead of just one. Further, the extend() method also works as an in-place operator, which means that we are going to have the same problem in using it as we had with the append() method. However, we can again make use of the side effect of list comprehension by dropping the use of assignment operator like we did above. Again, this is not a recommended method to use.

Python3




#list containing lists
squares = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]
  
#use the extend method inside list comprehension 
[square.extend([10]) for square in squares]
  
#print updated list
print(squares)


Output

[[1, 1, 10], [2, 4, 10], [3, 9, 10], [4, 16, 10], [5, 25, 10]]


Conclusion

In this article, we looked at the problem that exists when we try to append items to lists within a list comprehension using the append() method because it is an in-place operator. We further discussed how we can solve the problem using the Python ‘Or’ operator and the non in-place ‘+’ operator. Although doing this is quite simple with nested for loops, using a list comprehension might be necessary at a few places. Further, you can use the side effect of list comprehension if absolutely necessary. Hopefully, now you will be able to do it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads