list1 = input("list aval ra vared kon: ").split()
list2 = input("list dovom ra vared kon: ").split()
#I do not understand the following line
list1 [ : 0] = list2
print("javab is : " , list1)
Asked
Active
Viewed 56 times
0

sj95126
- 6,520
- 2
- 15
- 34
-
3It's the same thing as `list1 = list2 + list1`, but written by someone who thinks they're really clever. – Silvio Mayolo Aug 01 '22 at 04:39
-
2@SilvioMayolo: Technically, it's slightly different, because the first one modifies the existing `list1` and the second one replaces it with a new list. – sj95126 Aug 01 '22 at 04:44
1 Answers
0
The line of code: list1 [ : 0] = list2
adds all the value of list2
directly into list1
The values added are before the actual values of list1.
For Instance, take this example,
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = [1, 2, 3, 4, 5]
# Case 1
list1 [:0] = list2 # list1 = [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']
# Case 2
list1 [:1] = list2 # list1 = [1, 2, 3, 4, 5, 'b', 'c', 'd', 'e']
#Case 3
list1 [:2] = list2 # list1 = [1, 2, 3, 4, 5, 'c', 'd', 'e']
The syntax list[start:stop]
is used to slice lists into smaller parts. For more information, see this StackOverflow Post: Understanding slicing
The [:0]
shows that all the elements in list2
must be inserted into list1
before the 0th element(First Item in list1
)
Had you inserted [:1]
, the element before in list1
('a' -> list1[0]) would have been replaced with the values present in list2
Hope this helps.

Programmer-X31
- 19
- 4