What is the difference between list[0] & list[:1] - Python

Screen Link:

My Code:

apps_data[0].append('free_or_not')
print(apps_data[:6])

What I expected to happen:

apps_data[:1].append(‘free_or_not’)
print(apps_data[:6])

What actually happened:

Replace this line with the output/error

hi @tempax80

apps_data[0] will give you the exact row at index 0 that is the first row in the list. []

apps_data[:1] while in print will still show you the first row, however, it will be like a list of lists [[ ]].

list[0 : 1] denotes, from index position 0 up to index position 1 extract the rows. The upto here means till row at index 1 but not including it. So it still give us the first row but as list of list.

The following example might clear things up, let us know otherwise.

test_list = [["col1", "col2", "col3"],
         [1, "a", "first"],
         [2, "b", "second"],
         [3, "c", "third"]]

print("original list =", test_list, "\n")

print("test_list[0] : first row at index 0 = ", test_list[0], "\n")

print("test_list[:1] : rows starting from index 0 (not specified) upto index 1 =", test_list[:1], "\n")

print("test_list[0:2] : rows starting from index 0 (is specified) upto index 2 =", test_list[0:2])

The above code block will give you result like this: