Skip to content
This repository was archived by the owner on Oct 23, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions python/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,36 @@ List comprehensions are a powerful way of succinctly creating new lists. But be
responsible; if you find you’re doing something complicated, it’s probably
better to write a full `for` loop.

## List operations

Python also provides list methods that use familiar syntax.

The `+` operator is used to concatenate two lists.

```python
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> [1, 2, 3] + []
[1, 2, 3]
```

The `*` operator is used to make copies of elements inside a list.

```python
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> [1, 2, 3] * 1
[1, 2, 3]
>>> [1, 2, 3] * 0
[]
>>> [] * 3
[]
```

This behavior for `*` may be somewhat unintuitive, especially if you are thinking of lists like vectors or matricies, so keep it in mind when debugging your code.

Libraries like numpy provide array and matrix objects on which the `*` operator behaves as mathematically expected. Information about using modules and libraries is covered in the Modules section of this Python tutorial.

## Tuples

A close relative of lists are tuples, which differ in that they cannot be
Expand Down