'How to copy part of contents in one array to another?
I've got a list name users, and I want a second list named account_no to take only the first part of the contents of users.
users = [
'GB1520*Adam Johnson*07293105657',
'ZA5584*Dean Davids*07945671883'
]
account_no = []
def find_accountno():
for index in range(len(users)):
# I want to take the first 6 characters of users[index]
acc = users[index: 6]
account_no.append(acc)
print(users)
find_accountno()
print(account_no)
And this is the desired output:
['GB1520', 'ZA5584']
But, instead, I'm getting this:
[['GB1520*Adam Johnson*07293105657', 'ZA5584*Dean Davids*07945671883'], ['ZA5584*Dean Davids*07945671883']]
Solution 1:[1]
You should read a bit more about slicing; you'll see that it doesn't work the way you think it does.
You wrote:
acc = users[index: 6]
This is saying "take every element in users from index index to index 6 (including index, not including 6), form a new list from them, and put them in acc".
For example:
l = [0,1,2]
b = l[0:2]
Would have the list [0,1] inside b.
If what you want is to grab the first six characters of users[index], then you simply want users[index][0:6] (so users[index] is the string you wish to slice; then [0:6] employs slicing as described above to only grab the first 6 elements: 0 to 5). You can also drop the 0 (so [:6]).
Some extras:
Another two solutions, just to show you some fun alternatives (these use what's known as list comprehension):
def find_accountno_alt1():
numbers = [user[:6] for user in users]
account_no.extend(numbers)
def find_accountno_alt2():
numbers = [user.split('*')[0] for user in users]
account_no.extend(numbers)
Another point: I'd personally recommend simply passing the list (account_no) as a parameter to make the method neater and more self-contained.
Solution 2:[2]
In your code, you need to use acc=users[index][:6].
users = ['GB1520*Adam Johnson*07293105657', 'ZA5584*Dean Davids*07945671883']
account_no = []
def find_accountno():
for index in range(len(users)):
acc = users[index][:6] #I want to take the first 6 characters of users[index]
account_no.append(acc)
#print(users)
find_accountno()
print(account_no)
As for the multiple output, you are getting that because you are also printing the users list.
Solution 3:[3]
I suggest you to split the strings by "*" char and take only the first part (your account id)
account_no = [user.split("*")[0] for user in users]
EDIT: full code for your task
users = ['GB1520*Adam Johnson*07293105657', 'ZA5584*Dean Davids*07945671883']
account_no = [user.split("*")[0] for user in users]
print(users)
print(account_no)
Solution 4:[4]
Here is an alternative and more Pythonic way to write your code and get the results you want:
def find_account_numbers(users):
return [user[:6] for user in users]
users = [
'GB1520*Adam Johnson*07293105657',
'ZA5584*Dean Davids*07945671883'
]
account_numbers = find_account_numbers(users)
print(account_numbers)
The code snippet above will result in the following output:
['GB1520', 'ZA5584']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ErrorEliminator |
| Solution 3 | |
| Solution 4 | accdias |
