EzDevInfo.com

list interview questions

Top list frequently asked interview questions

Best way to check if a list is empty

For example, if passed the following:

a = []

How do I check to see if a is empty?


Source: (StackOverflow)

What's the difference between list and tuples?

What's the difference?

What are the advantages / disadvantages of tuples / lists?


Source: (StackOverflow)

Advertisements

How to clone or copy a list in Python?

What are the options to clone or copy a list in Python?

Using new_list = my_list then modifies new_list every time my_list changes. Why is this?


Source: (StackOverflow)

How can I count the occurrences of a list item in Python?

Given an item, how can I count its occurrences in a list in Python?


Source: (StackOverflow)

How to Sort a List by a property in the object

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class:

List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // fill list of orders

Now I want to sort the list based on one property of the Order object, for example I need to sort it by the order date or order id.

How can i do this in C#?


Source: (StackOverflow)

How to get the type of T from a generic List?

Let say I have a List<T> abc = new List<T>; inside a class public class MyClass<T>//....

Later, when I initialize the class, the T becomes MyTypeObject1. So I have a generic list, List< MyTypeObject1 >.

I would like to know, what type of object the list of my class contain, e.g. the list called abc contain what type of object?

I cannot do abc[0].GetType(); because the list might contain zero elements. How can I do it?


Source: (StackOverflow)

Using LINQ to remove elements from a List

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 I delete the Author elements from authorsList that are returned by the query into authors?

Or, put another way, how can I delete all of the firstname's equalling Bob from authorsList?

Note: This is a simplified example for the purposes of the question.


Source: (StackOverflow)

Python join, 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 specific reason it does it like this?


Source: (StackOverflow)

How to append list to second list (concatenate lists)

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]

Source: (StackOverflow)

How to get the size of a list

items = []
items.append("apple")
items.append("orange")
items.append("banana")

# FAKE METHOD::
items.amount()  # Should return 3

How do I do it right?


Source: (StackOverflow)

How to make a new List in Java

We create a Set as:

Set myset = new HashSet()

How do we create a List in Java?


Source: (StackOverflow)

Python - append vs. extend

What's the difference between the list methods append() and extend()?


Source: (StackOverflow)

Accessing the index in Python for loops

Does anyone know how to access the index itself for a list like this:

ints = [8, 23, 45, 12, 78]

When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?


Source: (StackOverflow)

Explain Python's slice notation

Do you have a good explanation (with references) 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 and am looking for a good guide.


Source: (StackOverflow)

Finding the index of an item given a list containing it in Python

For a list ["foo", "bar", "baz"] and an item in the list "bar", what's the cleanest way to get its index (1) in Python?


Source: (StackOverflow)