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,720
questions
379
votes
16
answers
681k
views
How do I subtract one list from another?
I want to take the difference between lists x and y:
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [1, 3, 5, 7, 9]
>>> x - y
# should return [0, 2, 4, 6, 8]
379
votes
10
answers
558k
views
How do I copy items from list to list without foreach?
How do I transfer the items contained in one List to another in C# without using foreach?
371
votes
7
answers
514k
views
How to empty a list?
It seems so "dirty" emptying a list in this way:
while len(alist) > 0 : alist.pop()
Does a clear way exist to do that?
367
votes
8
answers
691k
views
How to delete an item in a list if it exists?
I am getting new_tag from a form text field with self.response.get("new_tag") and selected_tags from checkbox fields with
self.response.get_all("selected_tags")
I combine them like this:
...
366
votes
20
answers
683k
views
How can I remove an element from a list?
I have a list and I want to remove a single element from it. How can I do this?
I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't ...
366
votes
10
answers
371k
views
How to merge lists into a list of tuples?
What is the Pythonic approach to achieve the following?
# Original lists:
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
# List of tuples from 'list_a' and 'list_b':
list_c = [(1,5), (2,6), (3,7), (4,...
365
votes
8
answers
528k
views
Check if two unordered lists are equal [duplicate]
I'm looking for an easy (and quick) way to determine if two unordered lists contain the same elements:
For example:
['one', 'two', 'three'] == ['one', 'two', 'three'] : true
['one', 'two', 'three'] ...
360
votes
15
answers
594k
views
Merge two (or more) lists into one, in C# .NET
Is it possible to convert two or more lists into one single list, in .NET using C#?
For example,
public static List<Product> GetAllProducts(int categoryId){ .... }
.
.
.
var ...
357
votes
5
answers
587k
views
Converting from IEnumerable to List [duplicate]
I want to convert from IEnumerable<Contact> to List<Contact>. How can I do this?
356
votes
2
answers
760k
views
List all files in one directory PHP [duplicate]
What would be the best way to list all the files in one directory with PHP? Is there a $_SERVER function to do this? I would like to list all the files in the usernames/ directory and loop over that ...
355
votes
14
answers
1.1m
views
How do I convert all of the items in a list to floats? [duplicate]
I have a script which reads a text file, pulls decimal numbers out of it as strings and places them into a list.
So I have this list:
my_list = ['0.49', '0.54', '0.54', '0.55', '0.55', '0.54', '0.55', ...
355
votes
12
answers
115k
views
Concatenating two lists - difference between '+=' and extend()
I've seen there are actually two (maybe more) ways to concatenate lists in Python:
One way is to use the extend() method:
a = [1, 2]
b = [2, 3]
b.extend(a)
the other to use the plus (+) operator:
b +=...
352
votes
4
answers
151k
views
Difference between List and Array types in Kotlin
What is the difference between List and Array types?
It seems can make same operations with them (loops, filter expression, etc..), is there any difference in behavior or usage?
val names1 = listOf("...
351
votes
13
answers
248k
views
How can I use list comprehensions to process a nested list?
I have this nested list:
l = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', ...
350
votes
14
answers
435k
views
List of Timezone IDs for use with FindTimeZoneById() in C#?
Can someone please point me to a complete list of all the timezones referenced by the id expected in TimeZoneInfo.FindTimeZoneById()? I can't find a list anywhere and I've looked through the .NET ...
349
votes
11
answers
653k
views
How to convert a Collection to List?
I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles.
My method is to retrieve a Collection of the values using:
Collection coll = themap....
349
votes
27
answers
451k
views
Efficient way to rotate a list in python
What is the most efficient way to rotate a list in python?
Right now I have something like this:
>>> def rotate(l, n):
... return l[n:] + l[:n]
...
>>> l = [1,2,3,4]
>>&...
346
votes
13
answers
219k
views
How to correctly use lists?
Brief background: Many (most?) contemporary programming languages in widespread use have at least a handful of ADTs [abstract data types] in common, in particular,
string (a sequence comprised of ...
344
votes
17
answers
452k
views
Element-wise addition of 2 lists?
I have now:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
I wish to have:
[1, 2, 3]
+ + +
[4, 5, 6]
|| || ||
[5, 7, 9]
Simply an element-wise addition of two lists.
I can surely iterate the two lists, ...
343
votes
9
answers
283k
views
Simultaneously merge multiple data.frames in a list
I have a list of many data.frames that I want to merge. The issue here is that each data.frame differs in terms of the number of rows and columns, but they all share the key variables (which I've ...
343
votes
11
answers
426k
views
Access multiple elements of list knowing their index [duplicate]
I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]. ...
340
votes
11
answers
668k
views
Conversion of System.Array to List
Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Array to List
Array ...
339
votes
13
answers
229k
views
In Python, when to use a Dictionary, List or Set?
When should I use a dictionary, list or set?
Are there scenarios that are more suited for each data type?
338
votes
9
answers
404k
views
Take the content of a list and append it to another list [duplicate]
I am trying to understand if it makes sense to take the content of a list and append it to another list.
I have the first list created through a loop function, that will get specific lines out of a ...
337
votes
5
answers
247k
views
Is the order of elements in a JSON list preserved?
I've noticed the order of elements in a JSON object not being the original order.
What about the elements of JSON lists? Is their order maintained?
333
votes
21
answers
382k
views
Split a List into smaller lists of N size [duplicate]
I am attempting to split a list into a series of smaller lists.
My Problem: My function to split lists doesn't split them into lists of the correct size. It should split them into lists of size 30 ...
332
votes
5
answers
425k
views
string.join(list) on object array rather than string array
In Python, I can do:
list = ['a', 'b', 'c']
', '.join(list) # 'a, b, c'
However, if I have a list of objects and try to do the same thing:
class Obj:
def __str__(self):
return 'name'
...
331
votes
11
answers
357k
views
remove None value from a list without removing the 0 value
This was my source I started with.
My List
L = [0, 23, 234, 89, None, 0, 35, 9]
When I run this :
L = filter(None, L)
I get this results
[23, 234, 89, 35, 9]
But this is not what I need, what ...
330
votes
3
answers
260k
views
str.startswith with a list of strings to test for
I'm trying to avoid using so many comparisons and simply use a list, but not sure how to use it with str.startswith:
if link.lower().startswith("js/") or link.lower().startswith("...
324
votes
18
answers
595k
views
Quickest way to compare two generic lists for differences
What is the quickest (and least resource intensive) to compare two massive (>50.000 items) and as a result have two lists like the ones below:
items that show up in the first list but not in the ...
321
votes
9
answers
141k
views
Are tuples more efficient than lists in Python?
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
317
votes
10
answers
436k
views
Getting a list of values from a list of dicts
I have a list of dicts like this:
[{'value': 'apple', 'blah': 2},
{'value': 'banana', 'blah': 3} ,
{'value': 'cars', 'blah': 4}]
I want ['apple', 'banana', 'cars']
Whats the best way to do this?...
316
votes
16
answers
402k
views
Test if a variable is a list or tuple
In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)
Is isinstance() as evil as suggested here? http://www.canonical.org/~kragen/isinstance/
Update: ...
316
votes
7
answers
270k
views
Python's most efficient way to choose longest string in list?
I have a list of variable length and am trying to find a way to test if the list item currently being evaluated is the longest string contained in the list. And I am using Python 2.6.1
For example:
...
316
votes
7
answers
208k
views
Check if string ends with one of the strings from a list
What is the pythonic way of writing the following code?
extensions = ['.mp3','.avi']
file_name = 'test.mp3'
for extension in extensions:
if file_name.endswith(extension):
#do stuff
I ...
315
votes
5
answers
528k
views
How to check if all elements of a list match a condition?
I have a list that contains many sub-lists of 3 elements each, like:
my_list = [["a", "b", 0], ["c", "d", 0], ["e", "f", 0], .....]
The ...
314
votes
25
answers
1.4m
views
How to print out all the elements of a List in Java?
I am trying to print out all the elements of a List, however it is printing the pointer of the Object rather than the value.
This is my printing code...
for(int i=0;i<list.size();i++){
System....
312
votes
22
answers
366k
views
Iterating over every two elements in a list [duplicate]
How do I make a for loop or a list comprehension so that every iteration gives me two elements?
l = [1,2,3,4,5,6]
for i,k in ???:
print str(i), '+', str(k), '=', str(i+k)
Output:
1+2=3
3+4=7
5+...
312
votes
29
answers
612k
views
Convert generic List/Enumerable to DataTable?
I have few methods that returns different Generic Lists.
Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection ...
311
votes
20
answers
354k
views
How do you cast a List of supertypes to a List of subtypes?
For example, lets say you have two classes:
public class TestA {}
public class TestB extends TestA{}
I have a method that returns a List<TestA> and I would like to cast all the objects in that ...
311
votes
10
answers
477k
views
Find elements in one list that are not in the other [duplicate]
I need to compare two lists in order to create a new list of specific elements found in one list but not in the other. For example:
main_list = []
list_1 = ["a", "b", "c",...
311
votes
17
answers
389k
views
vector vs. list in STL
I noticed in Effective STL that
vector is the type of sequence that
should be used by default.
What's does it mean? It seems that ignore the efficiency vector can do anything.
Could anybody ...
310
votes
12
answers
292k
views
Why do python lists have pop() but not push()
Does anyone know why Python's list.append method is not called list.push, given that there's already a list.pop that removes and returns the last element (indexed at -1) and list.append semantic is ...
309
votes
7
answers
659k
views
How do I loop through a list by twos? [duplicate]
I want to loop through a Python list and process 2 list items at a time. Something like this in another language:
for(int i = 0; i < list.length(); i+=2)
{
// do something with list[i] and list[...
307
votes
7
answers
709k
views
Getting all file names from a folder using C# [duplicate]
I wanted to know if it is possible to get all the names of text files in a certain folder.
For example, I have a folder with the name Maps, and I would like to get the names of all the text files in ...
306
votes
15
answers
559k
views
Convert a list to a string in C#
How do I convert a list to a string in C#?
When I execute toString on a List object, I get:
System.Collections.Generic.List`1[System.String]
306
votes
2
answers
253k
views
How to unzip a list of tuples into individual lists? [duplicate]
I have a list of tuples l = [(1,2), (3,4), (8,9)]. How can I, succinctly and Pythonically, unzip this list into two independent lists, to get [ [1, 3, 8], [2, 4, 9] ]?
In other words, how do I get the ...
306
votes
9
answers
162k
views
How is Python's List Implemented?
Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
305
votes
14
answers
329k
views
How can I verify if one list is a subset of another?
I need to verify if a list is a subset of another - a boolean return is all I seek.
Is testing equality on the smaller list after an intersection the fastest way to do this? Performance is of utmost ...
300
votes
20
answers
627k
views
How to convert List to Map?
Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so.
I want to know optimal conversion ...