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.
142,698
questions
890
votes
3
answers
992k
views
C# List<string> to string with delimiter
Is there a function in C# to quickly convert some collection to string and separate values with delimiter?
For example:
List<string> names --> string names_together = "John, Anna, Monica"
871
votes
17
answers
1.9m
views
How to check if a string is a substring of items in a list of strings
How do I search for items that contain the string 'abc' in the following list?
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
The following checks if 'abc' is in the list, but does not detect 'abc-...
861
votes
11
answers
668k
views
IEnumerable vs List - What to Use? How do they work?
I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects:
List<Animal> sel = (from animal in Animals
join race in Species
...
812
votes
11
answers
1.6m
views
Convert all strings in a list to integers [duplicate]
How do I convert all strings in a list to integers?
['1', '2', '3'] ⟶ [1, 2, 3]
811
votes
13
answers
1.1m
views
How do I convert a Map to List in Java?
How do I convert a Map<key,value> to a List<value>? Should I iterate over all map values and insert them into a list?
797
votes
5
answers
89k
views
Why is [] faster than list()?
I compared the processing speeds of [] and list() on Python 3.11
$ python -m timeit '[]'
20000000 loops, best of 5: 11.3 nsec per loop
$ python -m timeit 'list()'
10000000 loops, best of 5: 26.1 nsec ...
783
votes
10
answers
1.3m
views
Append integer to beginning of list in Python [duplicate]
How do I prepend an integer to the beginning of a list?
[1, 2, 3] ⟶ [42, 1, 2, 3]
779
votes
14
answers
861k
views
Using LINQ to remove elements from a List<T>
Say that I have LINQ query such as:
var authors = from x in authorsList
where x.firstname == "Bob"
select x;
Given that authorsList is of type List<Author>, how can ...
778
votes
8
answers
436k
views
How do I prepend to a short python list?
list.append() appends to the end of a list. This explains that list.prepend() does not exist due to performance concerns for large lists. For a short list, how do I prepend a value?
757
votes
30
answers
902k
views
How do I clone a generic list in C#?
I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone().
Is there an easy way around ...
756
votes
11
answers
1.4m
views
Convert list to array in Java [duplicate]
How can I convert a List to an Array in Java?
Check the code below:
ArrayList<Tienda> tiendas;
List<Tienda> tiendasList;
tiendas = new ArrayList<Tienda>();
Resources res = this....
755
votes
16
answers
418k
views
Array versus List<T>: When to use which?
MyClass[] array;
List<MyClass> list;
What are the scenarios when one is preferable over the other? And why?
732
votes
23
answers
588k
views
Java: convert List<String> to a join()d String
JavaScript has Array.join()
js>["Bill","Bob","Steve"].join(" and ")
Bill and Bob and Steve
Does Java have anything like this? I know I can cobble something ...
708
votes
43
answers
1.4m
views
How do I find the duplicates in a list and create another list with them?
How do I find the duplicates in a list of integers and create another list of the duplicates?
707
votes
22
answers
1.5m
views
Getting the index of the returned max or min item using max()/min() on a list
I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at ...
704
votes
15
answers
868k
views
How would you make a comma-separated string from a list of strings?
What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ['a', 'b', 'c'] to 'a,b,c'...
698
votes
40
answers
258k
views
How to iterate over a list in chunks
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list ...
690
votes
8
answers
1.2m
views
if else in a list comprehension [duplicate]
I have a list l:
l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
For numbers above 45 inclusive, I would like to add 1; and for numbers less than it, 5.
I tried
[x+1 for x in l if x >= 45 else x+5]
But ...
689
votes
25
answers
1.5m
views
Finding the average of a list
How do I find the arithmetic mean of a list in Python? For example:
[1, 2, 3, 4] ⟶ 2.5
687
votes
16
answers
1.2m
views
How do I split a string into a list of characters?
How do I split a string into a list of characters? str.split does not work.
"foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
680
votes
11
answers
1.1m
views
Alphabet range in Python
How do I create a list of alphabet characters, without doing it manually like this?
['a', 'b', 'c', 'd', ..., 'z']
675
votes
11
answers
333k
views
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
R provides two different methods for accessing the elements of a list or data.frame: [] and [[]].
What is the difference between the two, and when should I use one over the other?
651
votes
10
answers
2.0m
views
How do I split a string into a list of words?
How do I split a sentence and store each word in a list? e.g.
"these are words" ⟶ ["these", "are", "words"]
To split on other delimiters, see Split a ...
646
votes
19
answers
714k
views
Check if multiple strings exist in another string
How can I check if any of the strings in an array exists in another string?
For example:
a = ['a', 'b', 'c']
s = "a123"
if a in s:
print("some of the strings found in s")
else:
...
644
votes
11
answers
628k
views
Getting a map() to return a list in Python 3.x [duplicate]
I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:
A: Python 2.6:
>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
However, in Python 3....
642
votes
27
answers
1.2m
views
Convert a list to a data frame
I have a nested list of data. Its length is 132 and each item is a list of length 20. Is there a quick way to convert this structure into a data frame that has 132 rows and 20 columns of data?
Here is ...
641
votes
7
answers
1.6m
views
How can I get list of values from dict?
How can I get a list of the values in a dict in Python?
In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in ...
638
votes
20
answers
620k
views
Sorting list according to corresponding values from a parallel list [duplicate]
I have a list of strings like this:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
What is the shortest way of sorting X using values from Y to ...
637
votes
32
answers
849k
views
Remove duplicates from a List<T> in C#
Anyone have a quick method for de-duplicating a generic List in C#?
635
votes
15
answers
469k
views
Type List vs type ArrayList in Java [duplicate]
(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
I understand that with (1), implementations of the List interface can be swapped. It ...
623
votes
6
answers
774k
views
Is there a short contains function for lists?
Given a list xs and a value item, how can I check whether xs contains item (i.e., if any of the elements of xs is equal to item)? Is there something like xs.contains(item)?
For performance ...
621
votes
17
answers
646k
views
Why do I get an UnsupportedOperationException when trying to remove an element from a List?
I have this code:
public static String SelectRandomFromTemplate(String template,int count) {
String[] split = template.split("|");
List<String> list=Arrays.asList(split);
Random r = ...
618
votes
17
answers
2.4m
views
How do I declare an array in Python?
How do I declare an array in Python?
611
votes
15
answers
1.1m
views
Convert Set to List without creating new List
I am using this code to convert a Set to a List:
Map<String, List<String>> mainMap = new HashMap<>();
for (int i=0; i < something.size(); i++) {
Set<String> set = getSet(...
611
votes
13
answers
544k
views
Remove all the elements that occur in one list from another
Let's say I have two lists, l1 and l2. I want to perform l1 - l2, which returns all elements of l1 not in l2.
I can think of a naive loop approach to doing this, but that is going to be really ...
610
votes
28
answers
562k
views
How to remove elements from a generic list while iterating over it?
I am looking for a better pattern for working with a list of elements which each need processed and then depending on the outcome are removed from the list.
You can't use .Remove(element) inside a ...
606
votes
14
answers
208k
views
Transpose/Unzip Function (inverse of zip)?
I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item.
For example:
original = [('a', ...
594
votes
20
answers
807k
views
How to find all occurrences of an element in a list
index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element?
581
votes
40
answers
1.0m
views
How do I remove repeated elements from ArrayList?
I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?
580
votes
16
answers
604k
views
How to group dataframe rows into list in pandas groupby
Given a dataframe, I want to groupby the first column and get second column as lists in rows, so that a dataframe like:
a b
A 1
A 2
B 5
B 5
B 4
C 6
becomes
A [1,2]
B [5,5,4]
C [6]
How do I do this?
569
votes
11
answers
1.8m
views
Get list from pandas dataframe column or row?
I have a dataframe df imported from an Excel document like this:
cluster load_date budget actual fixed_price
A 1/1/2014 1000 4000 Y
A 2/1/2014 12000 10000 Y
A 3/1/2014 ...
568
votes
7
answers
249k
views
What are the advantages of NumPy over regular Python lists?
What are the advantages of NumPy over regular Python lists?
I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be ...
568
votes
13
answers
1.9m
views
How do I create a list with numbers between two values?
How do I create a list of numbers between two values? For example, a list between 11 and 16:
[11, 12, 13, 14, 15, 16]
561
votes
18
answers
239k
views
List<T> or IList<T> [closed]
Can anyone explain to me why I would want to use IList over List in C#?
Related question: Why is it considered bad to expose List<T>
560
votes
14
answers
1.2m
views
How to initialize a list of strings (List<string>) with many string values
How is it possible to initialize (with a C# initializer) a list of strings? I have tried with the example below but it's not working.
List<string> optionList = new List<string>
{
"...
558
votes
53
answers
210k
views
Flatten an irregular (arbitrarily nested) list of lists
Yes, I know this subject has been covered before:
Python idiom to chain (flatten) an infinite iterable of finite iterables?
Flattening a shallow list in Python
Comprehension for flattening a sequence ...
551
votes
12
answers
346k
views
HashSet vs. List performance
It's clear that a search performance of the generic HashSet<T> class is higher than of the generic List<T> class. Just compare the hash-based key with the linear approach in the List<T&...
549
votes
9
answers
546k
views
Sort a list of tuples by 2nd item (integer value) [duplicate]
I have a list of tuples that looks something like this:
[('abc', 121),('abc', 231),('abc', 148), ('abc',221)]
I want to sort this list in ascending order by the integer value inside the tuples. Is ...
545
votes
26
answers
850k
views
Remove all occurrences of a value from a list?
In Python remove() will remove the first occurrence of value in a list.
How to remove all occurrences of a value from a list?
This is what I have in mind:
>>> remove_values_from_list([1, 2,...
540
votes
7
answers
470k
views
List vs tuple, when to use each? [duplicate]
In Python, when should you use lists and when tuples?
Sometimes you don't have a choice, for example if you have
"hello %s you are %s years old" % x
then x must be a tuple.
But if I am the one who ...