filmov
tv
What is LIST in Python | Slicing and Indexing | Most Asked Interview Question | Learn Python

Показать описание
A list is a collection of items that are ordered and mutable. In other words, a list can store multiple items and you can change the order and values of the items in a list
for example.
here is the example of creating list. square brackets in Python denote a list and elements saperated by commna.
in list, Slicing allows you to grab a portion of the list and indexing allows you to access a specific element in the list.
for example If we want to grab the first three elements, we can use slicing like this: print into bracket list numbers in square bracket 0 index number : separeted by colon value 3 ])
The result will be [1, 2, 3]. If we want to access the second element, we can use indexing like this:
print(list_numbers[1])
The result will be 2.
To concatenate two lists, we can simply use the + operator. For example, let's say we have another list of numbers, list_numbers_2 = [6, 7, 8]. We can concatenate these two lists like this:
list_numbers_concat = list_numbers + list_numbers_2
The result will be combination of both.
lets talk about method in list.
Append method allows you to add an element at the end of the list. For example,
list dot append (9)
extend method allows you to add multiple elements to the end of the list. For example, list dot extend in bracket list element in square bracket you wanted to add.
insert method allows you to insert an element at a specific index. For example, list dot insert in bracket index position sapeted by value
pop method allows you to remove an element at the end of the list by default. For example, list dot pop bracket
other methods can be
sort method allows you to sort the elements in ascending order by default. For example,
The reverse method allows you to reverse the elements in the list. For example,
for example.
here is the example of creating list. square brackets in Python denote a list and elements saperated by commna.
in list, Slicing allows you to grab a portion of the list and indexing allows you to access a specific element in the list.
for example If we want to grab the first three elements, we can use slicing like this: print into bracket list numbers in square bracket 0 index number : separeted by colon value 3 ])
The result will be [1, 2, 3]. If we want to access the second element, we can use indexing like this:
print(list_numbers[1])
The result will be 2.
To concatenate two lists, we can simply use the + operator. For example, let's say we have another list of numbers, list_numbers_2 = [6, 7, 8]. We can concatenate these two lists like this:
list_numbers_concat = list_numbers + list_numbers_2
The result will be combination of both.
lets talk about method in list.
Append method allows you to add an element at the end of the list. For example,
list dot append (9)
extend method allows you to add multiple elements to the end of the list. For example, list dot extend in bracket list element in square bracket you wanted to add.
insert method allows you to insert an element at a specific index. For example, list dot insert in bracket index position sapeted by value
pop method allows you to remove an element at the end of the list by default. For example, list dot pop bracket
other methods can be
sort method allows you to sort the elements in ascending order by default. For example,
The reverse method allows you to reverse the elements in the list. For example,