Append nothing to list Python

Python list append[] method adds an element to the end of the list.

Python List append[] Syntax

The append[] method adds a single item to the existing list. The original list length is increased by 1. It is one of the most popular list methods.

The append[] method syntax is:

list.append[element]

The append[] method takes a single parameter, which is appended to the end of the list. Python list is mutable.

The element can be a number, string, object, list, etc. We can store different types of elements in a list.

List append[] return value

The list append[] method doesnt return anything. You can also say that the append[] method returns None.

Python List append[] Example

Lets look at a simple example to add an item to the end of the list.

vowels = ['a', 'e', 'i'] print[f'Original List is {vowels}'] vowels.append['o'] vowels.append['u'] print[f'Modified List is {vowels}']

Output:

Original List is ['a', 'e', 'i'] Modified List is ['a', 'e', 'i', 'o', 'u']

Appending List to another List

If we pass a list to the append[] method, it gets added as a single item to the end of the list.

list_numbers = [1, 2, 3] list_primes = [2, 3, 5, 7] list_numbers.append[list_primes] print[f'List after appending another list {list_numbers}']

Output:

List after appending another list [1, 2, 3, [2, 3, 5, 7]]

Tip: If you want to append the elements of a list to another list, use the list extend[] method.

list_numbers_odd = [1, 3, 5] list_numbers_even = [2, 4, 6, 8] list_numbers_odd.extend[list_numbers_even] print[f'List after extending from another list {list_numbers_odd}']

Output:

List after extending from another list [1, 3, 5, 2, 4, 6, 8]

Conclusion

Python List append[] method allows us to add any type of data to the end of the list. The method doesnt return anything. The original list is modified and the size is increased by 1.

References

  • Python add to List
  • Python.org Docs

Video liên quan

Chủ Đề