Travis build: 133
This commit is contained in:
@ -661,12 +661,12 @@ initialize_list_with_values(5, 2) # [2, 2, 2, 2, 2]
|
|||||||
|
|
||||||
Returns a list of elements that exist in both lists.
|
Returns a list of elements that exist in both lists.
|
||||||
|
|
||||||
Create a `set` from `b`, then use list comprehension on `a` to only keep values contained in both lists.
|
Create a `set` from `a` and `b`, then use the built-in set operator `&` to only keep values contained in both sets, then transform the `set` back into a `list`.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
def intersection(a, b):
|
def intersection(a, b):
|
||||||
_b = set(b)
|
_a, _b = set(a), set(b)
|
||||||
return [item for item in a if item in _b]
|
return list(_a & _b)
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|||||||
@ -558,14 +558,14 @@
|
|||||||
"type": "snippetListing",
|
"type": "snippetListing",
|
||||||
"title": "intersection",
|
"title": "intersection",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
"text": "Returns a list of elements that exist in both lists.\n\nCreate a `set` from `b`, then use list comprehension on `a` to only keep values contained in both lists.\n\n",
|
"text": "Returns a list of elements that exist in both lists.\n\nCreate a `set` from `a` and `b`, then use the built-in set operator `&` to only keep values contained in both sets, then transform the `set` back into a `list`.\n\n",
|
||||||
"tags": [
|
"tags": [
|
||||||
"list",
|
"list",
|
||||||
"beginner"
|
"beginner"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"hash": "a332cce7b7d7e303469e8c39c4926bf92ecb12847c3932985cc633e3a85a23bb"
|
"hash": "d20f757b18d4368324bfa74bb907f5dc2df9262886ea3722edd5015418ce282d"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -739,9 +739,9 @@
|
|||||||
"type": "snippet",
|
"type": "snippet",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
"fileName": "intersection.md",
|
"fileName": "intersection.md",
|
||||||
"text": "Returns a list of elements that exist in both lists.\n\nCreate a `set` from `b`, then use list comprehension on `a` to only keep values contained in both lists.\n\n",
|
"text": "Returns a list of elements that exist in both lists.\n\nCreate a `set` from `a` and `b`, then use the built-in set operator `&` to only keep values contained in both sets, then transform the `set` back into a `list`.\n\n",
|
||||||
"codeBlocks": {
|
"codeBlocks": {
|
||||||
"code": "def intersection(a, b):\n _b = set(b)\n return [item for item in a if item in _b]",
|
"code": "def intersection(a, b):\n _a, _b = set(a), set(b)\n return list(_a & _b)",
|
||||||
"example": "intersection([1, 2, 3], [4, 3, 2]) # [2, 3]"
|
"example": "intersection([1, 2, 3], [4, 3, 2]) # [2, 3]"
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
@ -750,7 +750,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"hash": "a332cce7b7d7e303469e8c39c4926bf92ecb12847c3932985cc633e3a85a23bb"
|
"hash": "d20f757b18d4368324bfa74bb907f5dc2df9262886ea3722edd5015418ce282d"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user