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.

See also:

Tags:

Links:

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

140640 questions
5249
votes
27 answers

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
5192
votes
33 answers

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
4319
votes
44 answers

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
3239
votes
31 answers

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
3228
votes
27 answers

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
3112
votes
20 answers

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
3069
votes
69 answers

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
2739
votes
22 answers

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
2709
votes
25 answers

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
2333
votes
18 answers

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
2251
votes
11 answers

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
2204
votes
18 answers

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
2125
votes
30 answers

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
2088
votes
26 answers

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
2065
votes
10 answers

Why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: ["Hello", "world"].join("-") Than this: "-".join(["Hello", "world"]) Is there a specific reason it is like this?
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
1
2 3
99 100