*In Python, a set is an unordered collection of unique elements. The append() method belongs to the List data type. If you try to call the append() method on a set to add elements to the set, you will raise the AttributeError: ‘set’ object has no attribute ‘append’. *

*To solve this error, you can use add() to add a single hashable element or update() to insert an iterable into a set. Otherwise, you can convert the set to a list then call the append() method.*

*This tutorial will go through how to solve the error with code examples.*

## Table of contents

## AttributeError: ‘set’ object has no attribute ‘append’

Letâ€™s break up the error message to understand what the error means. *AttributeError* occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “*‘set’ object has no attribute ‘append’*” tells us that the set object we are handling does not have the append method as an attribute.

The append method belongs to the list data type and appends an element to the end of the list.

The syntax of the `append()`

method is:

list.append(element)

**Parameters**

`element`

:*Required*. An element of any type to add to the end of the list.

Let’s look at an example of appending an element to a list:

lst = [2, 4, 6, 8, 10] lst.append(12) print(lst)

[2, 4, 6, 8, 10, 12]

## Example

Let’s look at an example where we try to append an element to a set. We can define a set in Python using curly brackets `{}`

.

a_set = {2, 4, 6, 8, 10} a_set.append(12) print(a_set)

Let’s run the code to see the result:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-22398ca5754b> in <module> 1 a_set = {2, 4, 6, 8, 10} 2 ----> 3 a_set.append(12) 4 5 print(a_set) AttributeError: 'set' object has no attribute 'append'

The error occurs because the set object does not have `append()`

as a method. The `append()`

method belongs to the list data type.

### Solution #1: Use a List Instead of a Set

We can solve this error by defining a list with square brackets `[]`

. Let’s look at the revised code:

a_list = [2, 4, 6, 8, 10] a_list.append(12) print(a_list)

[2, 4, 6, 8, 10, 12]

We can also convert a set to a list using the `list()`

method, append the element and then convert the list back to a set using the `set()`

method. Let’s look at the revised code:

a_set = {2, 4, 6, 8, 10} lst = list(a_set) lst.append(12) a_set = set(lst) print(a_set)

Let’s run the code to get the result:

{2, 4, 6, 8, 10, 12}

### Solution #2: Use the set.add() method

We can use the `add()`

method to add one item to a set. Note that a set is an unordered collection of items, the order of insertion is not recorded by the set. Let’s look at the revised code:

a_set = {2, 4, 6, 8, 10} a_set.add(12) print(a_set)

Let’s run the code to get the result:

{2, 4, 6, 8, 10, 12}

### Solution #3: Use the set.update() method

We can use the `update()`

method to add more than one item to a set. Note that a set is an unordered collection of items, the order of insertion is not recorded by the set. Let’s look at the revised code:

a_set = {2, 4, 6, 8, 10} # Define a list of elements elements_to_add = [12, 14, 16] # update the set with a list of elements a_set.update(elements_to_add) print(a_set) # Define a set of elements elements_to_add_2 = {18, 20, 22} # update the set with a set of elements a_set.update(elements_to_add_2) print(a_set)

Let’s run the code to see the result:

{2, 4, 6, 8, 10, 12, 14, 16} {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22}

## Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘set’ object has no attribute ‘append’ occurs when you call the append() method on a set.

To solve this error you can use the `set()`

methods `add()`

or `update()`

to add one or several items to a set, respectively. Otherwise, you can convert the set to a list using the list() method.

For further reading on AttributeErrors involving sets, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!