How to append a range to a list in Python

Append a range to a list in Python

To append a range to a list in Python, we have three methods: namely append, extend and += operator. Check out the details below to get more information.

Append a range to a list in Python

Using append()

This method can be used when you want to append something to a list. 

Syntax:

append(object)

Parameter: 

  • object: is the object which you want to append.

Code example:

The example below will create a range from 0 to 4 and append it to the list:

myList = ['learn', 'share', 'it']

# Append a range to a list 
myList.append(range(0,4))

print(myList) 

Output:

['learn', 'share', 'it', range(0, 4)]

Example 2: In this example, we will convert the range to a list and then append it to the myList.

myList = ['learn', 'share', 'it']

# Append a range to a list 
myList.append(list(range(0,4)))

print(myList)

Output:

['learn', 'share', 'it', [0, 1, 2, 3]]

Have you found the differences between each example? In the first example, we append a range object to the myList, which appears like range(0,4). In the second example, we convert the range object into a list of 4 elements and then append it to the list instead. Choosing what approach depends on what you exactly want. However, if these examples do not indicate what you want, it may be because you want to extend the list with the range. Therefore, you should look at the following solutions to see a different approach.

Using extend()

Another simple way to append a list with a range is to use extend(). Below is its syntax and usage:

Syntax:

extend(iterable)

Parameter: 

  • iterable: an iterable object such as a range or a list.

Example:

myList = ['learn', 'share', 'it']

# Append a range to a list 
myList.extend(range(0,4))

print(myList) 

Output:

['learn', 'share', 'it', 0, 1, 2, 3]

As you can see, if this approach produces the result that you are expecting for, maybe you have misunderstood the append method. Instead, you should want to use this method or the operator += in the next solution to append a range to a list in Python.

Using += operator

This operator is also called the concat operator in Python. When you apply this operator on a list in Python, it will merge the objects into the list and therefore you may refers it the same behavior as the extend() method:

Example:

myList = ['learn', 'share', 'it']

# Append a range to a list 
myList += range(0,4)

print(myList) 

Output:

['learn', 'share', 'it', 0, 1, 2, 3]

In fact, most programmers want to append a range to a list in this way because it is simpler and more effective.

Summary

In this tutorial, we have explained how to append a range to a list in Python. However, the third method is the most effective. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve our articles. Thanks for reading!

Maybe you are interested:

Leave a Reply

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