Albert van Zyl
Albert van Zyl

Reputation: 39

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']]

Upvotes: 1

Views: 769

Answers (4)

accdias
accdias

Reputation: 5372

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']

Upvotes: 1

Shay
Shay

Reputation: 617

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.

Upvotes: 3

Salvatore Daniele Bianco
Salvatore Daniele Bianco

Reputation: 2691

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)

Upvotes: 1

ErrorEliminator
ErrorEliminator

Reputation: 120

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.

Upvotes: 1

Related Questions