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?

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...

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 =...

XML Serialize generic list of serializable objects

Can I serialize a generic list of serializable objects without having to specify their type. Something like the intention behind the broken code below: List<ISerializable> serializableList = new...

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...

Working with dictionaries/lists to get list of keys

I have trivial question: I couldn't find a dictionary data structure in R, so I used list instead (like "word"->number). So, how do I get the list of keys.

Android list all images available

I am making an application which requires me to list all the images available on the SD-Card of the phone. I tried querying the ContentResolver way i.e. Cursor image =...

How to generate a random list of fixed length of values from given range?

How to generate a random (but unique and sorted) list of a fixed given length out of numbers of a given range in Python? Something like that: >>> list_length = 4 >>> values_range = [1, 30] >>>...

Print array without brackets and commas

I'm porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits my Android layout. How...

Check if all items are the same in a List

I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list. Thanks

Eliminate consecutive duplicates of list elements

Is there a "nice" way to eliminate *consecutive duplicates* of list elements? Example: ["red"; "red"; "blue"; "green"; "green"; "red"; "red"; "yellow"; "white"; "white"; "red"; "white"; "white"]...

Read all files in a folder and apply a function to each data frame

I am doing a relatively simple piece of analysis which I have put into a function, on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the...

Head and tail in one line

Is there a pythonic way to unpack a list in the first element and the "tail" in a single command? For example: >> head, tail = **some_magic applied to** [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>...

Create an empty list in Python with certain size

I want to create an empty list (or whatever is the best way) that can hold 10 elements. After that I want to assign values in that list, for example this is supposed to display 0 to 9: s1 =...

How to take product of two list in OCaml?

I have two lists : let a = ["a";"b"]; let b = ["c";"d"]; I want an output list c such as : c = ["a";"c";"a";"d";"b";"c";"b";"d"]; How to do it in ocaml as lists are immutable? I am new to it.

assign custom list by value memory allocation in c# list object assignment issue

objTempList = objParentList; foreach (var item in objTempList) { objChildList = objList.AsQueryable() .Where(x => x._parentId == item.filedbid) ...

preallocate list in R

It is inefficient in R to expand a data structure in a loop. How do I preallocate a list of a certain size? matrix makes this easy via the ncol and nrow arguments. How does one do this in lists?...

Why does a for-loop with pop-method (or del statement) not iterate over all list elements

I am new to Python and experimenting with lists I am using Python 3.2.3 (default, Oct 19 2012, 20:13:42), [GCC 4.6.3] on linux2 Here is my samplecode >>> l=[1,2,3,4,5,6] >>> for i in l: ... ...

How do I handle a variable number of arguments passed to a function in Racket?

I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation...

List shows 4 items in debugger even if filled with exactly one element

When I look at list populated with single item in debugger its _items field contains 4 elements. Can you explain the behavior? I've found that while debugging my console application to learn about...

Regarding immutable List (created by Arrays.asList())

When we create a list from an array using java.util.Arrays.asList(), the list is immutable. I am just curious to know why do we want to create a immutable list when the basic purpose of List (or...

How to save a list to a file and read it as a list type?

Say I have the list score=[1,2,3,4,5] and it gets changed whilst my program is running. How could I save it to a file so that next time the program is run I can access the changed list as a list...

Python : Operations with lists

Given a list like this: sentence = ("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper") and I want to have it like this: ["Alice Cooper", "George Kendall", "Joe Montana",...

Can not cast list<object> to list<custom object>

I have 3 class like below. [Serializable] public class Classa { public list<Object> list {get; set;} } [Serializable] public class Classb { public string name {get; set;} public string...

How to get max of dates list with stream, when having nulls in list

I have following code: List<LocalDate> dates = Arrays.asList(null, null, LocalDate.now(), LocalDate.now().minusDays(9)); LocalDate max = dates.stream() ...

Colorgram: removing Rgb from each element

I have this list: in which I would like to remove all the characters and leave the actual values such that I get the following: [(157, 155, 164),...,(56, 66, 70)] I tried s = [x.strip('Rgb(r=')...

Merge nested lists inside list of lists

I have a very huge list of the format: a = [[[5],[3]],[[4],[5]],[[6],[7]]] I want go get each element of my list out of innermost list. Output should be: output = [[5,3],[4,5],[6,7]] I tried...

Count the number of words that start with ā€˜Sā€™ in input_list

I want to count the number of words by making use of only the map function along with the lambda function Here is the code which I have written input_list = ['San Jose', 'San Francisco', 'Santa...

How to give the opposite of each numeral element of a list using Python

Is there any way in Python to give the opposite of each numeral element of a list in one line, without doing, for example: list_ = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for x in range(len(list_)): ...

returning empty list, append did not work

The question being: The email_list function receives a dictionary, which contains domain names as keys, and a list of users as values. Fill in the blanks to generate a list that contains complete...

Relevant tags