Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Modifying a set in Python

# initialize my_set
my_set = {1, 3}
print(my_set)
# my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing
# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)
# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)
# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
Comment

PREVIOUS NEXT
Code Example
Python :: defining a class in python 
Python :: fancy index python 
Python :: Python Decorating Functions with Parameters 
Python :: Python Printing negative timedelta object 
Python :: const obj = { a: 1, b: 2, c: 3, }; const obj2 = { ...obj, a: 0, }; console.log(obj2.a, obj2.b); 
Python :: can we use for loop inside if statement 
Python :: rich content field django ckeditor not showing bullets 
Python :: In interactive mode, the last printed expression is assigned to the variable _ 
Python :: pyspark percentage missing values 
Python :: NxN Array 
Python :: dataframe corr p value 
Python :: Python Windows Toggle Caps_Lock 
Python :: poisson random data 
Python :: set layer start and end time arcpy 
Python :: python datediff days 
Python :: stacked percentage bar chart 
Python :: time, date 
Python :: python SynC 
Python :: docker python no module named 
Python :: reemplazar un caracter de un string 
Python :: change label in dataframe per condition 
Python :: 2D list from dataframe column 
Python :: how to read file from terminal in python inside code 
Python :: asdfghjkl 
Python :: python cd to file 
Python :: give utton a number python 
Python :: moving element to the start ofa list python 
Python :: two legend left and right x asix matplotlib 
Python :: reportlab line thickness 
Python :: add a new categorical column to an existing table python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =