Example 1
# Creating a blank List
List = []
print(“Intial blank List: “)
print(List)
Output
Intial blank List:
[]
Добавление элементов в список
Элементы могут быть добавлены в список с помощью встроенной функции append(). Только один элемент за один раз может быть добавлен в список с помощью метода append(), для добавления нескольких элементов с помощью метода append() используются циклы.
Метод append () работает только для добавления элементов в конец списка.
Example 2
# Adding elements to the List
List.append(1)
List.append(2)
List.append(4)
print(“List after Addition of Three elements: “)
print(List)
Output
List after Addition of Three elements:
[1, 2, 4]
There’s one more method for Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list.
Example 3
# Adding multiple elements using Extend Method
List.extend([8, ‘Python’, ‘List’])
print(“List after performing Extend Operation: “)
print(List)
Output
List after performing Extend Operation:
[1, 2, 4, 8, ‘Python’, ‘List’]
For addition of element at the desired position, insert() method is used. Unlike append() which takes only one argument, insert() method requires two arguments (position, value).
Keep in mind
append() and extend() methods can only add elements at the end.
Example 4
# Adding element (using Insert Method)
List.insert(3, 12)
List2.insert(0, ‘Python’)
print(“List after performing Insert Operation: “)
print(List)
Output
List after performing Insert Operation:
[‘Python’, 1, 2, 4, 12, 8, ‘Python’, ‘List’]
For addition of multiple elements with the append() method, you can use loops.
Example 5
# Adding elements to the List using Iterator
for i in range(1, 4):
List.append(i)
print(“List after Adding elements from 1-3: “)
print(List)
Output
List after performing Insert Operation:
[‘Python’,1,2,4,12,8,‘Python’,‘List’,1,2,3]
Practice 1
1. Create empty list.
2. Add at least 3 numbers and 2 strings to the list
Practice 2
Insert new item to the list, that you’ve created in Practice1, between first and second items.
Practice 3
1. Create new blank list
2. Add EVEN numbers from 2 to 10 using loop.
Literacy
1. How many methods of adding elements to the list do you know?
2. What is the difference between append(), extend() and insert() methods?
Terminology
insert – кірістіру – вставить
element – элемент – элемент
extend – кеңейту – расширять
even – жұп – четный
odd – тақ – нечетный
3.3 SEARCH ELEMENT IN A LIST