Since QGIS 3.0 there is another approach available. Use the [`materialize()`](https://api.qgis.org/api/classQgsFeatureSource.html#ab95bcca99b1d5c082ca409da04e280b9) method from the [`QgsFeatureSource`](https://api.qgis.org/api/classQgsFeatureSource.html) class.
## Over the entire layer with the [`allFeatureIds()`](https://api.qgis.org/api/classQgsFeatureSource.html#ac25a91ac31808a8dc2b76026870fcf8b) method:
<!-- language: pyqgis -->
# imports
from qgis.utils import iface
from qgis.core import QgsFeatureRequest, QgsProject
# choose a layer
layer = iface.activeLayer()
# or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0]
# create a new layer from all features
new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.allFeatureIds()))
# add a new layer to the map
QgsProject.instance().addMapLayer(new_layer)
## Over the selection of all features with the [`selectedFeatureIds()`](https://api.qgis.org/api/classQgsVectorLayer.html#a62cf5c302ede7692ed049a1f1bcaec3f) method from the [`QgsVectorLayer`](https://api.qgis.org/api/classQgsVectorLayer.html) class:
<!-- language: pyqgis -->
# imports
from qgis.utils import iface
from qgis.core import QgsFeatureRequest, QgsProject
# choose a layer
layer = iface.activeLayer()
# or with: layer = QgsMapLayerRegistry.instance().mapLayersByName('name_of_the_layer')[0]
# select all features in the original layer
layer.selectAll()
# create a new layer from selected features
new_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))
# remove selection in the original layer
layer.removeSelection()
# add a new layer to the map
QgsProject.instance().addMapLayer(new_layer)
----------
**References:**
- https://gis.stackexchange.com/questions/80292/creating-vector-layer-from-selected-features-with-pyqgis