21 Answered Questions for the topic List

List Python


How do I check if a list is empty?

For example, if passed the following: a = []How do I check to see if `a` is empty?
List Python Slice


Understanding slice notation?

I need a good explanation (references are a plus) 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.


How to concatenate two lists in Python?

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]


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"] ... more


Removing duplicates in lists?

Pretty much I need to write a program to check if a list has any duplicates and if it does it removes them and returns a new list with the items that werent duplicated/removed. This is what I have... more
List Python


How can I reverse a list in Python?

How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= 0; i--)I need to have the elements of an array, but from the end to the beginning.

How do I sort a list of dictionaries by a value of the dictionary?

I have a list of dictionaries and want each item to be sorted by a specific property values.Take into consideration the array below, [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]When... more


How to clone or copy a list?

What are the options to clone or copy a list in Python?While using `new_list = my_list`, any modifications to `new_list` changes `my_list` everytime.Why is this?
List Python


How do I remove an element from a list by index in Python?

How do I remove an element from a list by index in Python?I found the `list.remove` method, but say I want to remove the last element, how do I do this? It seems like the default remove searches... more
List Python Tuples


What's the difference between lists and tuples?

What's the difference?What are the advantages / disadvantages of tuples / lists?

How to make a flat list out of list of lists?

I wonder whether there is a shortcut to make a simple list out of list of lists in Python.I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with *reduce*, but I get... more


Difference between append vs. extend list methods in Python?

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


In Python, how do you get the last element of a list?

list comprehension vs. lambda + filter?

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.My code looked like this: my_list = [x for x in my_list if x.attribute... more
List Python


How to get the number of elements in a list in Python?

items = [] items.append("apple") items.append("orange") items.append("banana") # FAKE METHOD:: items.amount() # Should return 3How do I get the number of elements in the list?
List Python


Is there a simple way to delete a list element by value?

a=[1,2,3,4] b=a.index(6) del a[b] print aThe above shows the following error:<!-- language: lang-none --> Traceback (most recent call last): File "D:\\zjm_code\\a.py", line... more


Convert two lists into a dictionary in Python?

Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam']What is the simplest way to produce the following dictionary? a_dict = {'name' : 'Monty', 'age' : 42,... more
List Python Random


How to randomly select an item from a list?

Assume I have the following list: foo = ['a', 'b', 'c', 'd', 'e']What is the simplest way to retrieve an item at random from this list?
List Python Count


How can I count the occurrences of a list item?

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


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"`, how do I get its index (1) in Python?
List Python Loops


Accessing the index in 'for' loops?

How do I access the index itself for a list like the following? 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?

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.