pythonic « List « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » List » pythonic 

1. list.append or list +=?    stackoverflow.com

Which is more pythonic?

list.append(1)
or
list += [1]

2. Creating lists of lists in a pythonic way    stackoverflow.com

I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.

mat=[[0]*2]*3
However, when I change the value of one of the ...

3. How to get the last element of a list?    stackoverflow.com

I found many different ways of getting the last element from a list in Python:

alist[-1]
alist[len(alist) -1]
How would you do this?

4. python list comprehensions; compressing a list of lists?    stackoverflow.com

guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do. What I'm doing is this. I ...

5. Pythonic List Comprehension    stackoverflow.com

This seems like a common task, alter some elements of an array, but my solution didn't feel very pythonic. Is there a better way to build urls with list comprehension?

links = ...

6. Pythonic way to get the largest item in a list    stackoverflow.com

Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care ...

7. more pythonic way of finding element in list that maximizes a function    stackoverflow.com

OK, I have this simple function that finds the element of the list that maximizes the value of another positive function.

def get_max(f, s):
    # f is a function ...

8. Concatenation of many lists in Python    stackoverflow.com

Suppose i have a function like this:

def getNeighbors(vertex)
which returns a list of vertices that are neighbors of the given vertex. Now i want to create a list with all the neighbors ...

9. Test if lists share any items in python    stackoverflow.com

I want to check if any of the items in one list are present in another list. I can do it simply with the code below, but I suspect there ...

10. Pythonic syntax for appending an arbitrary class object list property    stackoverflow.com

Is there an analog of setattr() that allows for appending an arbitrary list property of an instantiated class object? If not, is there a recommended way of doing so? This is a ...

11. why is python inconsistent when interpreting a subtraction when making a list?    stackoverflow.com

I am making a small program and at some point from each row of a matrix I need to subtract the average of the row itself. Quite a standard renormalization procedure. Note ...

12. Taking list's tail in a Pythonic way?    stackoverflow.com

from random import randrange
data = [(randrange(8), randrange(8)) for x in range(8)]
And we have to test if the first item equals to one of a tail. I am curious, how we would ...

13. How do I get a list of indices of non zero elements in a list?    stackoverflow.com

I have a list that will always contain only ones and zeroes. I need to get a list of the non-zero indices of the list:

a = [0, 1, 0, 1, 0, 0, ...

14. Is there a more pythonic way to find the point in a list which is closest to another point?    stackoverflow.com

I have a list of 2d points, and would like to find the one which is closest to a given point. The code (get_closest_point()) below does what I want. But is ...

15. Pythonic way of doing 'for these except myself'    stackoverflow.com

Basically, wondering what the best / most python-y way of doing this is;

class thing(collectionclass):
    for onething in super(collectionclass,listofthings):
        if onething != ...

16. The Zen of Python distils the guiding principles for Python into 20 aphorisms but lists only 19. What's the twentieth?    stackoverflow.com

From PEP 20, The Zen of Python:

Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 ...

17. Get specific object from a list with a certain parameter    stackoverflow.com

I have a list of Account objects in self.accounts, and I know that only one of them will have a type attribute equal to 'equity'. What is the best (most ...

18. What's the most Pythonic way to refactor this list building code?    stackoverflow.com

I have a list of results that I need to pull out various other lists from. For example, all owners that are male, all cars between 5 and 10 years old.

def ...

19. More Pythonic (or perhaps functional) way of creating this list?    stackoverflow.com

I'm returning a list of lists, but the following seems far more convoluted than it should be:

new_list = []
for key, value in group.items(): 
    new_list.extend([['%s%s%s%s%s' % (
  ...

20. Pythonic representation of a list of values with optional flags    stackoverflow.com

What first pops into my head is a dictionary with keys acting as list values and dictionary values defaulting to None, but this feels sub-optimal to me. Can anyone propose a ...

21. pythonic way of finding a value in a list which hasn't been supplied    stackoverflow.com

Here is an example of what I mean:

a = 0
b = 1

c = range(3)
so I would like to find the missing number in the list which in this case would be ...

22. Is it Pythonic to use list comprehensions for just side effects?    stackoverflow.com

Think about a function that I'm calling for it's side effects, not return values(like printing to screen, updating gui, printing to a file, etc.).

def fun_with_side_effects(x):
    ...side effects...
 ...

23. Python: Add item to list until a condition is true    stackoverflow.com

Normal list comprehensions occur this way:

new_list = [f(x) for x in l]
What is the most succinct and readable way to create new list in Python similar to this:
new_list = [f(x) while ...

24. Building a list of monthly totals    stackoverflow.com

I have a list of objects representing widgets. Each widget object has a manufactured DateTime field that holds the date and time when the widget was made. All the widgets in ...

25. Pythonic way of saying "if all of the elements in list 1 also exist in list 2"    stackoverflow.com

I want to return true from the if statement only if all of the elements from list 1 also exist in list 2 (list 2 is a superset of list 1). ...

26. Pythonic way to add each element in a list    stackoverflow.com

Possible Duplicate:
Making a flat list out of list of lists in Python
I have a list of lists: [['a word'], ['another world'], '['some more','words'], etc] And I ...

27. Pythonic way to mix two lists    stackoverflow.com

I have two lists of length n and n+1:

[a_1, a_2, ..., a_n]
[b_1, b_2, ..., b_(n+1)]
I want a function giving as a result a list with alternate elements from the two, that ...

28. Pythonic Infinite Lists    bytes.com

29. Pythonic way to analyze a list of files    python-forum.org

I am looking to analyze a list of files. I have a program that more or less uses os.walk() (I say more or less because it is actually a modified python version of the DOS tree command) that descends through a directory structure and lists the folders and the files that they contain. Now the files in these folders are in ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.