Python: How to Extend List with List using Slicing?
Note: python Extend will extend your given list with existing list at the end.
>>>a = [1,2,3] >>>a.extend([4,5]) >>>[1,2,3,4,5]
How to do the same using slicing?
at 0th position, middle or somewhere else it works as expected,
>>>l = [2,4,6,8,10] >>>l[:0] = [0, 1] >>>l >>>[0,1,2,4,6,8,10]
Even i have tried,
>>> l[len(l):] =  >>> l [2, 4, 6, 8, 10, 100] >>>
Is there better way without using function like len()?
but at the end its insert before last value.
>>> l[-1:-1] =  >>> l [2, 4, 6, 8, 100, 10]
>>>[2, 4, 6, 8, 10, 100]
I know using extend() method but i want to achieve using slicing at the end..
This works, though it's not as succint:
l[len(l):] = 
How to use list slicing to append to end of list in python?, If you really want to use slice, you can use the length of a: a = [5, 6] a[len(a):] = [1, 2, 3] a. output: [5, 6, 1, 2, 3]. But the simplest is to directly� After getting the list, we can get a part of it using python’s slicing operator which has the following syntax: [start : stop : steps] which means that slicing will start from index start will go up to stop in step of steps.
In short, not exactly, because of how slices work (emphasis mine):
The slice of s from i to j is defined as the sequence of items with index k such that
i <= k < j. If i or j is greater than
len(s). If i is omitted or
0. If j is omitted or
len(s). If i is greater than or equal to j, the slice is empty.
Nevertheless, note that
lists actually have a maximum size, which we can find with
sys.maxsize. If we use that as the start of the
slice, in conjunction with the emphasised part above, we can actually do something that will work for all
import sys END = sys.MAXSIZE a = [1, 2, 3] b = [4, 5] a[END:] = b print(a)
[1, 2, 3, 4, 5]
Of course, practically speaking, just use
Add an item to a list in Python (append, extend, insert), In Python, use list methods append() , extend() , and insert() to add items to a list or combine other lists. You can also use the + operator to combine lists, or use slices to insert itemss at specific positions. Slice notation in short: [ <first element to include> : <first element to exclude> : <step> ] If you want to include the first element when reversing a list, leave the middle element empty, like this: foo[::-1] You can also find some good information about Python slices in general here: Explain Python's slice notation
+= which isn't using slicing, but is one of the ways without using an inbuilt function like len
In : a = [1,2,3] In : a += [4,5] In : a Out: [1, 2, 3, 4, 5]
To use slicing, the python docs already provide a solution, also noted in Amadan's answer . https://docs.python.org/3/tutorial/datastructures.html
list.extend(iterable) . Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
15 Extended Slices, However, Python's built-in list, tuple, and string sequence types have never as a list or an array you can assign to or delete an extended slice, but there are� If you need to add an element to the end of a list, you can use the append () method. a1 = [1, 2] a2 = [1, 2] b = (3, 4) # a1 = [1, 2, 3, 4] a1.extend (b) print (a1) # a2 = [1, 2, (3, 4)] a2.append (b) print (a2) Output. [1, 2, 3, 4] [1, 2, (3, 4)] To learn more, visit list append () method.
As per document shared by @Devesh,
Data Strutures it extend your list we need length of list.
so we can use built in function len() to do it in simple way.
otherwise we can use sys.maxsize
>>> l = [2,4,6,8,10] >>> import sys >>> l[sys.maxsize:] = [100, 200] >>> l [2, 4, 6, 8, 10, 100, 200] >>>
One more Solution,
Seems little better,
>>> l = [2,4,6,8,10] >>> l[-1:] = l[-1:]+[100, 200, 300] >>> l [2, 4, 6, 8, 10, 100, 200, 300] >>>
this is another method i have tried using
>>> l = [2,4,6,8,10] >>> l.reverse() >>> l[:0] = [3,5,7][::-1] >>> l [7, 5, 3, 10, 8, 6, 4, 2] >>> l.reverse() >>> l [2, 4, 6, 8, 10, 3, 5, 7] >>>
Extending a list in Python (5 different ways), When we want to add more elements to a list, the extension of list happens. This can We can use slicing to add elements at the end of a list. Slicing in Python When you want to extract part of a string, or some part of a list, you use a slice The first character in string x would be x and the nth character would be at x[n-1]. Python also indexes the arrays backwards, using negative numbers. The last character has index -1, the second to last character has index -2.
Python lists - working with lists in Python, In this part of the Python programming tutorial, we cover Python lists in more detail. The extend() method adds a sequence of values to the end of a list. In List slicing is an operation that extracts certain elements from a list� Python list method extend() appends the contents of seq to list. Syntax. Following is the syntax for extend() method − list.extend(seq) Parameters. seq − This is the list of elements. Return Value. This method does not return any value but add the content to existing list. Example. The following example shows the usage of extend() method.
Slicing lists helps in fetching sections (slices) of the list. This is a very useful command to get a partial list from a parent list. lst [start:end] # Items from index=start to index=end-1 lst [start:] # Items index=start through the rest of the array lst [:end] # All items from the beginning through index=end-1 lst [:] # A copy of the whole array
List slicing. Python comes with a magical slice operator which returns the part of a sequence. It operates on objects of different data types such as strings, tuples, and works the same on a Python list. Slicing syntax. It has a mystic syntax, which is as follows.
- The extended slice must start after the last element. The imaginary element after the last element has a positive index of L (the length of the list), but it does not have a negative index. You cannot solve your problem without knowing the length of the list.
- @DYZ yes got it... to extend at the end i should need length.. otherwise i can use without length.. docs.python.org/3/tutorial/datastructures.html
- I know this, is there better way? without using function like len()?
- Even the docs have the same approach: docs.python.org/3/tutorial/datastructures.html
list.extend(iterable) Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.@SilentSpectator
- this is informative.. and we can try using import sys; sys.maxsize
- @SilentSpectator Yup, I checked the
sixdocs and it is equal to
sys.maxsize. I actually edited my answer right before your comment!
- How is this using slicing?
- this is concatenation bro.. its not slicing