Snippets tagged “list”
71 snippets use this tag.
- A Java collection of value pairs? (tuples?)Java
In Java, you can use the AbstractMap.SimpleEntry class from the java.util package to represent a value pair (also known as a tuple).
- Alternatives for returning multiple values from a Python functionPython
Using a tuple:
- Append integer to beginning of list in PythonPython
To append an integer to the beginning of a list in Python, you can use the insert() method.
- ArrayList of int array in javaJava
To create an ArrayList of int arrays in Java, you can use the following syntax:
- Calling remove in foreach loop in JavaJava
If you want to remove elements from a collection while iterating over it using a for-each loop in Java, you must use an iterator instead of the looping construct.
- Convert all strings in a list to intPython
In Python, you can use the map() function along with the built-in int() function to convert all strings in a list to integers.
- Convert list to array in JavaJava
To convert a List to an array in Java, you can use the toArray() method of the List interface. This method returns an array containing all of the elements in the list in the proper order.
- Convert list to tuple in PythonPython
You can convert a list to a tuple in Python by using the built-in tuple() function.
- Convert Set to List without creating new ListJava
To convert a Set to a List in Java without creating a new List object, you can use the List constructor that takes a Collection as an argument.
- Create an empty list with certain size in PythonPython
You can create an empty list of a certain size in Python using the following methods:
- Difference between del, remove, and pop on listsPython
del: The del keyword is used to remove an item from a list by its index.
- Does java.util.List.isEmpty() check if the list itself is null?Java
No, the isEmpty() method of the java.util.List interface does not check if the list itself is null.
- Easiest way to convert a List to a Set in JavaJava
To convert a List to a Set in Java, you can use the new HashSet<>(list) constructor to create a new HashSet and initialize it with the elements of the List.
- Finding and replacing elements in a listPython
Here is a Python code snippet that demonstrates how to find and replace elements in a list:
- Finding the index of an item in a listPython
To find the index of an item in a list in Python, you can use the index() method of the list.
- Get a list from Pandas DataFrame column headersPython
You can use the DataFrame.columns attribute to access the column labels of a DataFrame as an Index object.
- Get list from pandas dataframe column or row?Python
In Pandas, a DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.
- Get list of JSON objects with Spring RestTemplateJava
To get a list of JSON objects using the Spring RestTemplate, you can use the exchange() method to send an HTTP GET request to the server, and then use the getBody() method of the ResponseEntity object to retrieve the list of objects.
- HashMap with multiple values under the same keyJava
To store multiple values under the same key in a HashMap in Java, you can use a List or an array as the value for the key.
- How can I convert List<Integer> to int[] in Java?Java
You can use the toArray() method of the List interface to convert a List<Integer> to an int[] in Java.
- How can I get list of values from dict?Python
You can use the values() method to get a list of values from a dictionary:
- How can I sort a List alphabetically?Java
To sort a List alphabetically in Java, you can use the Collections.sort method and pass in your List as an argument.
- How do I clone a list so that it doesn't change unexpectedly after assignment?Python
There are a few different ways to make a copy of a list in Python:
- How do I concatenate two lists in Python?Python
There are a few ways to concatenate lists in Python.
- How do I convert a Map to List in Java?Java
You can use the entrySet() method of the Map interface to get a Set view of the mappings contained in the map, and then create a List from this set using the new
- How do I count the occurrences of a list item?Python
There are a few ways you can count the occurrences of a list item in Python:
- How do I create a list with numbers between two values?Python
Here is an example of how you might create a list of numbers between two values in python:
- How do I determine whether an array contains a particular value in Java?Java
To check if an array contains a particular value in Java, you can use the contains() method of the List interface, which is implemented by the ArrayList class.
- How do I find the duplicates in a list and create another list with them?Python
In Python, one way to find duplicates in a list and create another list with them is to use a for loop and an if statement.
- How do I get the last element of a list?Python
To get the last element of a list, you can use negative indexing.
- How do I join two lists in Java?Java
There are several ways to join two lists in Java. Here are a few options:
- How do I remove the first item from a list?Python
You can use the list method pop(index) to remove the first item in a list.
- How do I reverse a list or loop over it backwards?Python
There are a few ways to reverse a list in Python, depending on what you want to do with the reversed list.
- How to add new elements to an array in Java?Java
To add new elements to an array in Java, you have a few options: If you know the size of the array in advance and the array is not full, you can simply assign a new value to an unused element in the array.
- How to convert a Collection to List?Java
In Java, you can convert a Collection (such as Set, Queue, etc.) to a List using the following methods:
- How to convert array to list in JavaJava
To convert an array to a list in Java, you can use the Arrays.asList() method. This method returns a fixed-size list backed by the specified array. Here's an example:
- How to convert comma-separated String to List?Java
To convert a comma-separated String to a List in Java, you can use the String.split() method to split the string into an array of substrings, and then use the Arrays.asList() method to create a list from that array.
- How to convert int[] into List<Integer> in Java?Java
To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.
- How to convert List to Map?Java
To convert a List to a Map in Java, you can use the stream() method and the collect() method along with a Collectors.toMap() call. Here's an example of how to do this:
- How to count the number of occurrences of an element in a ListJava
To count the number of occurrences of an element in a List in Java, you can use the Collections.frequency(Collection, Object) method, which returns the number of times the specified element appears in the collection.
- How to declare an ArrayList with values?Java
To declare an ArrayList with values in Java, you can use the Arrays.asList method and pass it an array or a list of values.
- How to declare and add items to an array in Python?Python
To declare an array in Python, you can use the array module.
- How to get the first element of the List or Set?Java
To get the first element of a List or Set in Java, you can use the following methods:
- How to initialize List<String> object in Java?Java
To initialize a List<String> object in Java, you can use one of the following approaches: Using the new keyword:
- How to print out all the elements of a List in Java?Java
To print out all the elements of a List in Java, you can use a for loop or an enhanced for loop.
- How to read a file line-by-line into a list?Python
To read a file line-by-line into a list, you can use the following approach:
- How to remove an element from a list by indexPython
To remove an element from a list by index in Python, you can use the pop() method.
- How to remove items from a list while iterating?Python
It is generally not recommended to remove items from a list while iterating over it because it can cause unexpected behavior.
- How to sort a list of objects based on an attribute of the objects?Python
You can use the sort method of a list and pass in a key argument, which is a function that takes an object and returns the value on which you want to sort the list.
- How to use Comparator in Java to sortJava
To use a Comparator in Java to sort a list of objects, you can use the Collections.sort method and pass it a List and a Comparator.
- How to use Jackson to deserialise an array of objectsJava
Jackson is a powerful Java library for processing JSON data. You can use Jackson to deserialize an array of objects by following these steps:
- In Python, how do I convert all of the items in a list to floats?Python
You can use a for loop and the float() function to convert each item in a list to a float.
- Is there a simple way to delete a list element by value?Python
Yes, you can use the remove() method to delete a list element by its value.
- Java 8 List<V> into Map<K>Java
To convert a List<V> into a Map<K> in Java 8, you can use the toMap() method of the Collectors class from the java.util.stream package.
- Java ArrayList copyJava
There are several ways to create a copy of an ArrayList in Java:
- Java Compare Two ListsJava
To compare two lists in Java, you can use the equals() method of the List interface.
- Java List.contains(Object with field value equal to x)Java
To check if a Java List contains an object with a specific field value, you can use the List.stream().anyMatch() method along with a lambda expression to filter the elements in the list based on the field value.
- Java: convert List<String> to a join()d StringJava
To convert a List<String> to a single string by joining the elements of the list with a separator, you can use the join() method of the java.util.StringJoiner class.
- Java: Get first item from a collectionJava
To get the first item from a collection in Java, you can use the iterator() method to get an iterator for the collection, and then call the next() method on the iterator to get the first element.
- List of lists changes reflected across sublists unexpectedlyPython
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 listPython
You can use the len() function to count the number of elements in a list.
- Python list of dictionaries searchPython
Here is a code snippet that demonstrates how to search for a specific value in a list of dictionaries in Python:
- Python: Find in listPython
Here is a code snippet that demonstrates how to find an element in a list in Python:
- Python: finding an element in a listPython
To find an element in a list, you can use the in keyword.
- Shuffling a list of objectsPython
The random module in Python provides a function called shuffle() which can be used to shuffle the elements of a list.
- Traverse a list in reverse order in PythonPython
You can use the reversed() function to iterate through a list in reverse order.
- Ways to iterate over a list in JavaJava
There are several ways to iterate over a List in Java. Here are some of the most common approaches:
- What is the difference between Python's list methods append and extend?Python
The append method adds an item to the end of a list.
- What is the difference between Set and List?Java
In Java, a Set is an interface that extends the Collection interface.
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?Java
The java.lang.UnsupportedOperationException is thrown when an operation is not supported by a class.
- Why is there no SortedList in Java?Java
There is no SortedList class in the Java standard library because the List interface already provides a way to store elements in a specific order.