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.

117280 questions
4124
votes
22 answers

Accessing the index in 'for' loops?

How do I access the index in a for loop like the following? ints = [8, 23, 45, 12, 78] for i in ints: print('item #{} = {}'.format(???, i)) I want to get this output: item #1 = 8 item #2 = 23 item #3 = 45 item #4 = 12 item #5 = 78 When I loop…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
4074
votes
51 answers

How to make a flat list out of a list of lists?

Is there a shortcut to make a simple list out of a list of lists in Python? I can do it in a for loop, but maybe there is some cool "one-liner"? I tried it with functools.reduce() from functools import reduce l = [[1, 2, 3], [4, 5, 6], [7], [8,…
Emma
  • 41,365
  • 4
  • 17
  • 10
3827
votes
33 answers

Understanding slice notation

I need a good explanation (references are a plus) on Python's slice notation. To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it.
Simon
  • 70,546
  • 25
  • 83
  • 117
3629
votes
33 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) in Python?
Eugene M
  • 40,509
  • 14
  • 35
  • 43
3231
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
  • 169,974
  • 95
  • 213
  • 200
3114
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
  • 206,738
  • 150
  • 445
  • 651
2946
votes
27 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
  • 59,444
  • 25
  • 58
  • 84
2850
votes
22 answers

List changes unexpectedly after assignment. Why is this and how can I prevent it?

While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it?
aF.
  • 58,798
  • 40
  • 127
  • 191
2562
votes
66 answers

How do you split a list into evenly sized chunks?

I have a list of arbitrary length, and I need to split it up into equal size chunks and operate on it. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and…
jespern
  • 27,388
  • 3
  • 21
  • 12
2402
votes
16 answers

Getting the last element of a list

In Python, how do you get the last element of a list?
Janusz
  • 176,216
  • 111
  • 293
  • 365
2202
votes
17 answers

How do I sort a list of dictionaries by a value of the dictionary?

I have a list of dictionaries and want each item to be sorted by a specific value. Take into consideration the list: [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] When sorted by name, it should become: [{'name':'Bart', 'age':10},…
masi
  • 118
  • 3
  • 5
  • 9
2065
votes
10 answers

How do I get the number of elements in a list?

Consider the following: items = [] items.append("apple") items.append("orange") items.append("banana") # FAKE METHOD: items.amount() # Should return 3 How do I get the number of elements in the list items?
y2k
  • 59,444
  • 25
  • 58
  • 84
1972
votes
15 answers

How to randomly select an item from a list?

Assume I have the following list: foo = ['a', 'b', 'c', 'd', 'e'] What is the simplest way to retrieve an item at random from this list?
Ray
  • 169,974
  • 95
  • 213
  • 200
1876
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: my_list = ["Hello", "world"] print(my_list.join("-")) # Produce: "Hello-world" Than this: my_list = ["Hello", "world"] print("-".join(my_list)) # Produce: "Hello-world" Is there a…
Evan Fosmark
  • 89,147
  • 33
  • 99
  • 116
1766
votes
18 answers

How to remove an element from a list by index

How do I remove an element from a list by index in Python? I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
1
2 3
99 100