0

I'm am fairly new to python but not new to programming. I am using Python version 2.7.5.

I have a list, A, where each entry contains ('string1', 'string2') and I have M of these elements. I have another list, B,(where each entry also contains ('string1', 'string2')) that is much smaller then list A and has N elements. I want to keep the first N elements of list A and get rid of the rest. How would I do that? Any help would be greatly appreciated.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
user2604504
  • 697
  • 2
  • 14
  • 29
  • Can you include some sample code in the question so we have a better idea of input and output? – mhlester May 01 '14 at 18:01
  • er, what is the point for address List B here? I know it has N elements, but do you mean you want to keep the those same N elements in A? – Jerry Meng May 01 '14 at 18:01
  • do you want to find difference between two list? –  May 01 '14 at 18:19
  • Have you solved the issue? If yes, consider accepting any answer that you think deserve. – alecxe May 08 '14 at 16:17

4 Answers4

1

You should use the built-in function len() and slicing here:

>>> A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> B = [10, 54]
>>> A = A[len(B):]
>>> A
[3, 4, 5, 6, 7, 8, 9]

Here are some examples of slicing and len():

len():

>>> lst = [1, 2, 3, 6, 5]
>>> len(lst)
5
>>> lst.append(7)
>>> lst.append(4)
>>> len(lst)
7
>>> lst
[1, 2, 3, 6, 5, 7, 4]

slicing:

>>> lst = [1, 5, 7, 4, 3, 2]
>>> lst[3:5]
[4, 3]
>>> lst[3:]
[4, 3, 2]
>>> lst[:3]
[1, 5, 7]
>>> lst[:3] = ''
>>> lst
[4, 3, 2]

As you can see in the end of the top code, slicing can also be used to remove certain parts of lists.

Hope this helps!

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

len and list slicing is your solution here.

new_A = A[:len(B)]

len(B) will give you the length of the list B, and slicing the list works like:

list[start=0 : end=len(list) (: step)]

This also works with strings :)

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

You can "slice" the first list based on the length of a second:

>>> l1 = [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]
>>> l2 = [('what', 'a'), ('wonderful', 'world')]
>>> l1[:len(l2)]
[('a', 'b'), ('c', 'd')]
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Generally, please try your hardest to provide actual code examples.

However from your question it seems you want to truncate the longer list to the length of the shorter list.

Lists can be controlled using the [i:j] (slice) notation in python, where i is the start index and j the end index, for the new list

for example

>>> A = [1, 2, 3, 4]
>>> A[:2]
[1, 2]
>>> 

For your case, you want A truncated to the length of B (which can be found using len(B) ) simply:

A = A[:len(B)]

I hope this answers your question, slice is gone into more detail here.

Community
  • 1
  • 1
Tehsmeely
  • 188
  • 6
  • `A = A[:len(B)]` reassigns the name `A` to the slice, but does not affect the list that `A` previously pointed to. To truncate the list we can replace the remainder slice with empty: `A[len(B):] = []` – nmclean May 01 '14 at 18:30
  • Both are valid techniques, get to the other end with a list named A that is equal to A at the start but the same length as B. We needn't worry about such things. – Tehsmeely May 01 '14 at 18:33
  • No, they are different techniques that do different things. It depends whether OP wants to modify the list or create a new one. The only time we needn't worry about such things is if `A` is the only reference to that list in the entire application. For a simple demonstration consider `def truncate(lst): lst = lst[:2]`. Call `truncate(A)` and it will fail. If we define the function as `lst[2:] = []` it will succeed. – nmclean May 01 '14 at 19:06