Python list methods
Append()
- Adds an element at the end of the list.
symbols = ['π΅', 'π¨', 'π¨']
symbols.append('πΊ')
# output
['π΅', 'π¨', 'π¨', 'πΊ']
Extend()
- Add the elements of a list (or any iterable), to the end of the current list.
symbols = ['π΅', 'π¨', 'π¨']
add_forms = ['πΊ', 'πΆ']
symbols.extend(add_forms)
# output
['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ']
Insert()
- Adds an element at the specified position.
symbols = ['π΅', 'π¨', 'π¨']
symbols.insert(0, 'πΊ')
# output
['πΊ', 'π΅', 'π¨', 'π¨']
Remove()
- Removes the first item with the specified value.
symbols = ['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ']
symbols.remove('π¨')
# output
['π΅', 'π¨', 'πΊ', 'πΆ']
Pop()
- Removes the element at the specified position.
symbols = ['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ']
symbols.pop(0)
# output
['π¨', 'π¨', 'πΊ', 'πΆ']
Clear()
- Removes all the elements from the list.
symbols = ['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ']
symbols.clear()
# output
[]
Sort()
- Sorts the list.
symbols = ['π΅', 'π¨', 'πΊ', 'πΆ']
symbols.sort()
# output
['π΅', 'πΆ', 'πΊ', 'π¨']
Reverse()
- Reverses the order of the list.
symbols = ['π΅', 'π¨', 'πΊ', 'πΆ']
symbols.reverse()
# output
['πΆ', 'πΊ', 'π¨', 'π΅']
Index()
- Returns the index of the first element with the specified value.
symbols = ['π΅', 'π¨', 'πΊ', 'πΆ']
x = symbols.index('πΆ')
# output
3
Count()
- Returns the number of elements with the specified value.
symbols = ['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ']
x = symbols.count('π¨')
# output
2
copy()
- Returns a copy of the list.
symbols = ['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ', 'πΆ']
x = symbols.copy()
# output
['π΅', 'π¨', 'π¨', 'πΊ', 'πΆ', 'πΆ']
All the best to you.
Top comments (0)