+

# Python | Assign a range of items to a list

Method # 1: Using `extend()`
This can be solved with an extension function, where the pasting of a range of numbers can be done at the trailing end with using the range function.

 ` # Python3 demo code ` ` # Assign a range of elements to a list ` ` # using extend () `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 3 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 8 ` `] ` ` `  ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) `   ` # using extend () ` ` # Assigning a range of items to a list ` ` test_list .extend (` ` range ` ` (` ` 6 ` `)) `   ` # print result ` ` print ` ` (` ` "The list after adding range elements:" ` ` + ` ` str ` ` ( test_list)) `

Output:

` The original list: [3, 5, 6, 8] The list after adding range elements: [3, 5, 6, 8, 0, 1, 2, 3, 4, 5] `

Method # 2 Using the `*` operator
This problem can also be solved using the * operator and with the advantage of being flexible about adding elements at any position.

 ` # Python3 demo code ` ` # Assign a range of items to a list ` ` # using the * operator `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 3 ` `, ` ` 5 ` , ` 6 ` `, ` ` 8 ` `] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) `   ` # using the * operator ` ` # Purpose range of elements to list ` ` res ` ` = ` ` [ ` ` 3 ` `, ` ` 5 ` `, ` ` * ` ` range ` ` (` ` 6 ` `), ` ` 6 ` `, ` ` 8 ] ``   # print result print ( "The list after adding range elements: " + str (res)) `

Output:

` The original list: [3, 5 , 6, 8] The list after adding range elements: [3, 5, 0, 1, 2, 3, 4, 5, 6, 8] `