How-to articles, tricks, and solutions about LIST

List comprehension vs. lambda + filter

List comprehension and the combination of lambda functions and the filter() function in Python are both used to filter a list and return a new list with only the elements that satisfy a certain condition.

List of lists changes reflected across sublists unexpectedly

In Python, when you create a list of lists and modify one of the sublists, the change is reflected in all other sublists as well because they are all pointing to the same object in memory.

Python - Count elements in list

You can use the len() function to count the number of elements in a list.

Python - TypeError: 'int' object is not iterable

This error message is indicating that you are trying to iterate over an object of type 'int', which is not iterable (i.e.

Python: Find in list

Here is a code snippet that demonstrates how to find an element in a list in Python:

Python: finding an element in a list

To find an element in a list, you can use the in keyword.

Remove empty strings from a list of strings

This code uses a list comprehension to iterate through the original list and only keep the elements that are not empty strings.

Removing duplicates in lists

There are a few ways to remove duplicates from a list in Python.

Shuffling a list of objects

The random module in Python provides a function called shuffle() which can be used to shuffle the elements of a list.

stale element reference: element is not attached to the page document

The "stale element reference" error in Selenium WebDriver occurs when an element that was previously found on the webpage is no longer attached to the DOM (Document Object Model) and is therefore no longer accessible through the browser.

Sum a list of numbers in Python

Here is a code snippet that demonstrates how to sum a list of numbers in Python:

Type List vs type ArrayList in Java

In Java, List is an interface that defines a list data structure, while ArrayList is a class that implements the List interface.

TypeError: list indices must be integers or slices, not str

This error is usually caused when you try to access an element in a list using a string as the index, rather than an integer.

What is the difference between Set and List?

In Java, a Set is an interface that extends the Collection interface.

What's the difference between lists and tuples?

Lists and tuples are both used to store multiple items in a single variable, but they are different in a few key ways.

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

The java.lang.UnsupportedOperationException is thrown when an operation is not supported by a class.

Why is it string.join(list) instead of list.join(string)?

The join() method is a string method, so it is called on a string object and takes a list of strings as its argument.

Writing a list to a file with Python, with newlines

Here is a code snippet that demonstrates how to write a list to a file with newlines in Python:

1 2 3 4