# Questions tagged [list]

The list tag may refer to: a linked list (an ordered set of nodes, each referencing its successor), or a form of dynamic array. Not to be used for HTML lists, use [html-lists] instead.

Specific Implementations

Almost every programming language provides support for list data structures. They are particularly common in functional languages, because their inductive properties lend themselves to elegant iterative and recursive abstractions.

Performance:

The flexibility of linked lists is somewhat offset by the complexity of some operations on lists (accessing an element at index n in a linked list is O(n), while for arrays it is O(1)). In practice, linked lists are a very disadvantageous structure for random-access-based algorithms. Linked lists support quick O(1) appends, dynamic arrays also support O(1) appends however this O(1) runtime is amortized as it must factor in the periodical re-sizing of the dynamic array. The performance of lists and arrays is of the same order (O) for operations that deal with every element of the list in order.

Types

Lists are the canonical recursive (or inductive) data type.

Tags:

Do not use this tag for unordered/ordered lists in HTML, use instead.

140640 questions

### Accessing the index in 'for' loops

How do I access the index while iterating over a sequence with a for loop? xs = [8, 23, 45] for x in xs: print("item #{} = {}".format(index, x)) Desired output: item #1 = 8 item #2 = 23 item #3 = 45
Joan Venge
• 315,713
• 212
• 479
• 689

### How do I make a flat list out of a list of lists?

I have a list of lists like [ [1, 2, 3], [4, 5, 6], [7], [8, 9] ] How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]? If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly…
Emma
• 52,713
• 4
• 19
• 10

### Finding the index of an item in a list

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1?
Eugene M
• 47,557
• 14
• 38
• 44

### How do I concatenate two lists in Python?

How do I concatenate two lists in Python? Example: listone = [1, 2, 3] listtwo = [4, 5, 6] Expected outcome: >>> joinedlist [1, 2, 3, 4, 5, 6]
y2k
• 65,388
• 27
• 61
• 86

### How do I check if a list is empty?

For example, if passed the following: a = [] How do I check to see if a is empty?
Ray
• 187,153
• 97
• 222
• 204

### What is the difference between Python's list methods append and extend?

What's the difference between the list methods append() and extend()?
Claudiu
• 224,032
• 165
• 485
• 680

### How do I split a list into equally-sized chunks?

How do I split a list of arbitrary length into equal sized chunks? See also: How to iterate over a list in chunks. To chunk strings, see Split string every nth character?.
jespern
• 32,466
• 3
• 23
• 12

### How to sort a list of dictionaries by a value of the dictionary in Python?

How do I sort a list of dictionaries by a specific key's value? Given: [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}] When sorted by name, it should become: [{'name': 'Bart', 'age': 10}, {'name': 'Homer', 'age': 39}]
masi
• 118
• 3
• 5
• 9

### How do I get the last element of a list?

How do I get the last element of a list? Which way is preferred? alist[-1] alist[len(alist) - 1]
Janusz
• 187,060
• 113
• 301
• 369

### How can I randomly select (choose) an item from a list (get a random element)?

How do I retrieve an item at random from the following list? foo = ['a', 'b', 'c', 'd', 'e']
Ray
• 187,153
• 97
• 222
• 204

### How do I get the number of elements in a list (length of a list) in Python?

How do I get the number of elements in the list items? items = ["apple", "orange", "banana"] # There are 3 items.
y2k
• 65,388
• 27
• 61
• 86

### How to remove an element from a list by index

How do I remove an element from a list by index? I found list.remove(), but this slowly scans the list for an item by value.
Joan Venge
• 315,713
• 212
• 479
• 689

### How do I count the occurrences of a list item?

Given a single item, how do I count occurrences of it in a list, in Python? A related but different problem is counting occurrences of each different element in a collection, getting a dictionary or list as a histogram result instead of a single…
weakish
• 28,682
• 5
• 48
• 60

### How do I check if a variable is an array in JavaScript?

How do I check if a variable is an array in JavaScript? if (variable.constructor == Array)
Andy McCluggage
• 37,618
• 18
• 59
• 69